1+ from tkinter import *
2+
3+ dis_msg = ''
4+
5+ def bhan (arg ):
6+ global dis_msg
7+
8+ if arg == '=' :
9+ t1 .set (str (eval (dis_msg )))
10+ elif arg == 'ce' :
11+ dis_msg = ''
12+ t1 .set ('cleared' )
13+ else :
14+ dis_msg += arg
15+ t1 .set (dis_msg )
16+
17+
18+ mwin = Tk ()
19+ mwin .title ("My Calculator" )
20+ #text area as display
21+ t1 = StringVar ()
22+ e1 = Entry (mwin ,textvariable = t1 )
23+ e1 .grid (row = 0 ,column = 0 ,columnspan = 4 )
24+
25+ # Buttons
26+ b1 = Button (mwin ,text = '1' ,command = lambda : bhan ('1' ))
27+ b2 = Button (mwin ,text = '2' ,command = lambda : bhan ('2' ))
28+ b3 = Button (mwin ,text = '3' ,command = lambda : bhan ('3' ) )
29+ b4 = Button (mwin ,text = '4' ,command = lambda : bhan ('4' ))
30+ b5 = Button (mwin ,text = '5' ,command = lambda : bhan ('5' ))
31+ b6 = Button (mwin ,text = '6' ,command = lambda : bhan ('6' ))
32+ b7 = Button (mwin ,text = '7' ,command = lambda : bhan ('7' ) )
33+ b8 = Button (mwin ,text = '8' ,command = lambda : bhan ('8' ))
34+ b9 = Button (mwin ,text = '9' ,command = lambda : bhan ('9' ) )
35+ b0 = Button (mwin ,text = '0' ,command = lambda : bhan ('0' ))
36+ bp = Button (mwin ,text = '+' ,command = lambda : bhan ('+' ))
37+ bm = Button (mwin ,text = '-' ,command = lambda : bhan ('-' ))
38+ bclr = Button (mwin ,text = 'CE' ,command = lambda : bhan ('ce' ))
39+ be = Button (mwin ,text = '=' ,command = lambda : bhan ('=' ))
40+
41+ b1 .grid (row = 1 ,column = 0 )
42+ b2 .grid (row = 1 ,column = 1 )
43+ b3 .grid (row = 2 ,column = 0 )
44+ b4 .grid (row = 2 ,column = 1 )
45+ b5 .grid (row = 3 ,column = 0 )
46+ b6 .grid (row = 3 ,column = 1 )
47+ b7 .grid (row = 4 ,column = 0 )
48+ b8 .grid (row = 4 ,column = 1 )
49+ b9 .grid (row = 5 ,column = 0 )
50+ b0 .grid (row = 5 ,column = 1 )
51+ bp .grid (row = 1 ,column = 2 )
52+ bm .grid (row = 1 ,column = 3 )
53+ bclr .grid (row = 2 ,column = 2 )
54+ be .grid (row = 2 ,column = 3 )
55+
56+ mwin .mainloop ()
0 commit comments