1+ #importing modules
12import tkinter as tk
23from tkinter import *
4+ #defining attributes of main window
35root = tk .Tk ()
46root .geometry ("170x230" )
57root .title ("Calculator" )
1214
1315
1416# <==================== Button Operation code starts here.. ==============>
15- def nine ():
16- inp .insert ("end" ,"9" )
1717
18- def eight ():
19- inp .insert ("end" ,"8" )
20-
21- def seven ():
22- inp .insert ("end" ,"7" )
23-
24- def six ():
25- inp .insert ("end" ,"6" )
26-
27- def five ():
28- inp .insert ("end" ,"5" )
29-
30- def four ():
31- inp .insert ("end" ,"4" )
32-
33- def three ():
34- inp .insert ("end" ,"3" )
35-
36- def two ():
37- inp .insert ("end" ,"2" )
38-
39- def one ():
40- inp .insert ("end" ,"1" )
41-
42- def zero ():
43- inp .insert ("end" ,"0" )
44-
45- def double_zero ():
46- inp .insert ("end" ,"00" )
47-
48- def dot ():
49- inp .insert ("end" ,"." )
50-
51- def plus ():
52- inp .insert ("end" ,"+" )
53-
54- def minus ():
55- inp .insert ("end" ,"-" )
56-
57- def mul ():
58- inp .insert ("end" ,"*" )
59-
60- def divide ():
61- inp .insert ("end" ,"/" )
62-
63- def modulus ():
64- inp .insert ("end" ,"%" )
6518
6619def result ():
67-
68-
69- if inp .get () == "" :
70- inp .insert ("end" ,"error" )
71- elif inp .get ()[0 ] == "0" :
72- inp .delete (0 ,"end" )
73- inp .insert ("end" ,"error" )
74-
75- else :
20+ try :
21+
22+ if inp .get () == "" :
23+ inp .insert ("end" ,"error" )
24+ elif inp .get ()[0 ] == "0" :
25+ inp .delete (0 ,"end" )
26+ inp .insert ("end" ,"error" )
27+
28+ else :
7629 res = inp .get ()
7730 res = eval (res )
7831 inp .insert ("end" ," = " )
7932 inp .insert ("end" ,res )
80-
81- def clear ():
82- inp .delete (0 ,"end" )
83-
84-
33+ except SyntaxError :
34+ inp .insert ("end" ,"invalid input" )
8535# <============ end code ================>
8636
8737
8838
8939# <============= Button Design Code starts here.. ==================>
90-
91- clear = Button (root ,text = "C" ,width = 2 ,command = clear ,bg = "red" ,fg = "white" ,relief = RIDGE )
40+ # using lambda instead of functions to make the use of buttons more clear
41+ clear = Button (root ,text = "C" ,width = 2 ,command = lambda : inp . delete ( 0 , "end" ) ,bg = "red" ,fg = "white" ,relief = RIDGE )
9242clear .grid (row = 0 ,sticky = "w" ,padx = 125 )
9343
9444
95- nine = Button (text = "9" ,width = 2 ,command = nine ,borderwidth = 3 ,relief = RIDGE )
45+ nine = Button (text = "9" ,width = 2 ,command = lambda : inp . insert ( "end" , "9" ) ,borderwidth = 3 ,relief = RIDGE )
9646nine .grid (row = 1 ,sticky = "w" ,padx = 15 )
9747
98- eight = Button (text = "8" ,width = 2 ,command = eight ,borderwidth = 3 ,relief = RIDGE )
48+ eight = Button (text = "8" ,width = 2 ,command = lambda : inp . insert ( "end" , "8" ) ,borderwidth = 3 ,relief = RIDGE )
9949eight .grid (row = 1 ,sticky = "w" ,padx = 45 )
10050
101- seven = Button (root ,text = "7" ,width = 2 ,command = seven ,borderwidth = 3 ,relief = RIDGE )
51+ seven = Button (root ,text = "7" ,width = 2 ,command = lambda : inp . insert ( "end" , "7" ) ,borderwidth = 3 ,relief = RIDGE )
10252seven .grid (row = 1 ,sticky = "w" ,padx = 75 )
10353
104- plus = Button (root ,text = "+" ,width = 2 ,command = plus ,borderwidth = 3 ,relief = RIDGE )
54+ plus = Button (root ,text = "+" ,width = 2 ,command = lambda : inp . insert ( "end" , "+" ) ,borderwidth = 3 ,relief = RIDGE )
10555plus .grid (row = 1 ,sticky = "e" ,padx = 125 )
10656
10757
108- six = Button (text = "6" ,width = 2 ,command = six ,borderwidth = 3 ,relief = RIDGE )
58+ six = Button (text = "6" ,width = 2 ,command = lambda : inp . insert ( "end" , "6" ) ,borderwidth = 3 ,relief = RIDGE )
10959six .grid (row = 2 ,sticky = "w" ,padx = 15 ,pady = 5 )
11060
111- five = Button (text = "5" ,width = 2 ,command = five ,borderwidth = 3 ,relief = RIDGE )
61+ five = Button (text = "5" ,width = 2 ,command = lambda : inp . insert ( "end" , "5" ) ,borderwidth = 3 ,relief = RIDGE )
11262five .grid (row = 2 ,sticky = "w" ,padx = 45 ,pady = 5 )
11363
114- four = Button (root ,text = "4" ,width = 2 ,command = four ,borderwidth = 3 ,relief = RIDGE )
64+ four = Button (root ,text = "4" ,width = 2 ,command = lambda : inp . insert ( "end" , "4" ) ,borderwidth = 3 ,relief = RIDGE )
11565four .grid (row = 2 ,sticky = "w" ,padx = 75 ,pady = 5 )
11666
117- minus = Button (root ,text = "-" ,width = 2 ,command = minus ,borderwidth = 3 ,relief = RIDGE )
67+ minus = Button (root ,text = "-" ,width = 2 ,command = lambda : inp . insert ( "end" , "-" ) ,borderwidth = 3 ,relief = RIDGE )
11868minus .grid (row = 2 ,sticky = "e" ,padx = 125 ,pady = 5 )
11969
12070
12171
122- three = Button (text = "3" ,width = 2 ,command = three ,borderwidth = 3 ,relief = RIDGE )
72+ three = Button (text = "3" ,width = 2 ,command = lambda : inp . insert ( "end" , "3" ) ,borderwidth = 3 ,relief = RIDGE )
12373three .grid (row = 3 ,sticky = "w" ,padx = 15 ,pady = 5 )
12474
125- two = Button (text = "2" ,width = 2 ,command = two ,borderwidth = 3 ,relief = RIDGE )
75+ two = Button (text = "2" ,width = 2 ,command = lambda : inp . insert ( "end" , "2" ) ,borderwidth = 3 ,relief = RIDGE )
12676two .grid (row = 3 ,sticky = "w" ,padx = 45 ,pady = 5 )
12777
128- one = Button (root ,text = "1" ,width = 2 ,command = one ,borderwidth = 3 ,relief = RIDGE )
78+ one = Button (root ,text = "1" ,width = 2 ,command = lambda : inp . insert ( "end" , "1" ) ,borderwidth = 3 ,relief = RIDGE )
12979one .grid (row = 3 ,sticky = "w" ,padx = 75 ,pady = 5 )
13080
131- multiply = Button (root ,text = "*" ,width = 2 ,command = mul ,borderwidth = 3 ,relief = RIDGE )
81+ multiply = Button (root ,text = "*" ,width = 2 ,command = lambda : inp . insert ( "end" , "*" ) ,borderwidth = 3 ,relief = RIDGE )
13282multiply .grid (row = 3 ,sticky = "e" ,padx = 125 ,pady = 5 )
13383
13484
135- zero = Button (text = "0" ,width = 2 ,command = zero ,borderwidth = 3 ,relief = RIDGE )
85+ zero = Button (text = "0" ,width = 2 ,command = lambda : inp . insert ( "end" , "0" ) ,borderwidth = 3 ,relief = RIDGE )
13686zero .grid (row = 4 ,sticky = "w" ,padx = 15 ,pady = 5 )
13787
138- double_zero = Button (text = "00" ,width = 2 ,command = double_zero ,borderwidth = 3 ,relief = RIDGE )
88+ double_zero = Button (text = "00" ,width = 2 ,command = lambda : inp . insert ( "end" , "00" ) ,borderwidth = 3 ,relief = RIDGE )
13989double_zero .grid (row = 4 ,sticky = "w" ,padx = 45 ,pady = 5 )
14090
141- dot = Button (root ,text = "." ,width = 2 ,command = dot ,borderwidth = 3 ,relief = RIDGE )
91+ dot = Button (root ,text = "." ,width = 2 ,command = lambda : inp . insert ( "end" , "." ) ,borderwidth = 3 ,relief = RIDGE )
14292dot .grid (row = 4 ,sticky = "w" ,padx = 75 ,pady = 5 )
14393
144- divide = Button (root ,text = "/" ,width = 2 ,command = divide ,borderwidth = 3 ,relief = RIDGE )
94+ divide = Button (root ,text = "/" ,width = 2 ,command = lambda : inp . insert ( "end" , "/" ) ,borderwidth = 3 ,relief = RIDGE )
14595divide .grid (row = 4 ,sticky = "e" ,padx = 125 ,pady = 5 )
14696
14797result = Button (root ,text = "=" ,width = 10 ,command = result ,bg = "red" ,fg = "white" ,borderwidth = 3 ,relief = RIDGE )
14898result .grid (row = 5 ,sticky = "w" ,padx = 15 ,pady = 5 )
14999
150- modulus = Button (root ,text = "%" ,width = 2 ,command = modulus ,borderwidth = 3 ,relief = RIDGE )
100+ modulus = Button (root ,text = "%" ,width = 2 ,command = lambda : inp . insert ( "end" , "%" ) ,borderwidth = 3 ,relief = RIDGE )
151101modulus .grid (row = 5 ,sticky = "e" ,padx = 125 ,pady = 5 )
152102
153103root .mainloop ()
154104
155- # <============ end code ==============>
105+ # <============ end code ==============>
0 commit comments