1+ import tkinter as tk
2+ from tkinter import *
3+ root = tk .Tk ()
4+ root .geometry ("170x230" )
5+ root .title ("Calculator" )
6+ root .maxsize (170 ,230 )
7+ root .minsize (170 ,230 )
8+
9+ #Entry Widgets to show calculations
10+ inp = Entry (root ,width = 16 ,borderwidth = 3 ,relief = RIDGE )
11+ inp .grid (pady = 10 ,row = 0 ,sticky = "w" ,padx = 15 )
12+
13+
14+ # <==================== Button Operation code starts here.. ==============>
15+ def nine ():
16+ inp .insert ("end" ,"9" )
17+
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" ,"%" )
65+
66+ def 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 :
76+ res = inp .get ()
77+ res = eval (res )
78+ inp .insert ("end" ," = " )
79+ inp .insert ("end" ,res )
80+
81+ def clear ():
82+ inp .delete (0 ,"end" )
83+
84+
85+ # <============ end code ================>
86+
87+
88+
89+ # <============= Button Design Code starts here.. ==================>
90+
91+ clear = Button (root ,text = "C" ,width = 2 ,command = clear ,bg = "red" ,fg = "white" ,relief = RIDGE )
92+ clear .grid (row = 0 ,sticky = "w" ,padx = 125 )
93+
94+
95+ nine = Button (text = "9" ,width = 2 ,command = nine ,borderwidth = 3 ,relief = RIDGE )
96+ nine .grid (row = 1 ,sticky = "w" ,padx = 15 )
97+
98+ eight = Button (text = "8" ,width = 2 ,command = eight ,borderwidth = 3 ,relief = RIDGE )
99+ eight .grid (row = 1 ,sticky = "w" ,padx = 45 )
100+
101+ seven = Button (root ,text = "7" ,width = 2 ,command = seven ,borderwidth = 3 ,relief = RIDGE )
102+ seven .grid (row = 1 ,sticky = "w" ,padx = 75 )
103+
104+ plus = Button (root ,text = "+" ,width = 2 ,command = plus ,borderwidth = 3 ,relief = RIDGE )
105+ plus .grid (row = 1 ,sticky = "e" ,padx = 125 )
106+
107+
108+ six = Button (text = "6" ,width = 2 ,command = six ,borderwidth = 3 ,relief = RIDGE )
109+ six .grid (row = 2 ,sticky = "w" ,padx = 15 ,pady = 5 )
110+
111+ five = Button (text = "5" ,width = 2 ,command = five ,borderwidth = 3 ,relief = RIDGE )
112+ five .grid (row = 2 ,sticky = "w" ,padx = 45 ,pady = 5 )
113+
114+ four = Button (root ,text = "4" ,width = 2 ,command = four ,borderwidth = 3 ,relief = RIDGE )
115+ four .grid (row = 2 ,sticky = "w" ,padx = 75 ,pady = 5 )
116+
117+ minus = Button (root ,text = "-" ,width = 2 ,command = minus ,borderwidth = 3 ,relief = RIDGE )
118+ minus .grid (row = 2 ,sticky = "e" ,padx = 125 ,pady = 5 )
119+
120+
121+
122+ three = Button (text = "3" ,width = 2 ,command = three ,borderwidth = 3 ,relief = RIDGE )
123+ three .grid (row = 3 ,sticky = "w" ,padx = 15 ,pady = 5 )
124+
125+ two = Button (text = "2" ,width = 2 ,command = two ,borderwidth = 3 ,relief = RIDGE )
126+ two .grid (row = 3 ,sticky = "w" ,padx = 45 ,pady = 5 )
127+
128+ one = Button (root ,text = "1" ,width = 2 ,command = one ,borderwidth = 3 ,relief = RIDGE )
129+ one .grid (row = 3 ,sticky = "w" ,padx = 75 ,pady = 5 )
130+
131+ multiply = Button (root ,text = "*" ,width = 2 ,command = mul ,borderwidth = 3 ,relief = RIDGE )
132+ multiply .grid (row = 3 ,sticky = "e" ,padx = 125 ,pady = 5 )
133+
134+
135+ zero = Button (text = "0" ,width = 2 ,command = zero ,borderwidth = 3 ,relief = RIDGE )
136+ zero .grid (row = 4 ,sticky = "w" ,padx = 15 ,pady = 5 )
137+
138+ double_zero = Button (text = "00" ,width = 2 ,command = double_zero ,borderwidth = 3 ,relief = RIDGE )
139+ double_zero .grid (row = 4 ,sticky = "w" ,padx = 45 ,pady = 5 )
140+
141+ dot = Button (root ,text = "." ,width = 2 ,command = dot ,borderwidth = 3 ,relief = RIDGE )
142+ dot .grid (row = 4 ,sticky = "w" ,padx = 75 ,pady = 5 )
143+
144+ divide = Button (root ,text = "/" ,width = 2 ,command = divide ,borderwidth = 3 ,relief = RIDGE )
145+ divide .grid (row = 4 ,sticky = "e" ,padx = 125 ,pady = 5 )
146+
147+ result = Button (root ,text = "=" ,width = 10 ,command = result ,bg = "red" ,fg = "white" ,borderwidth = 3 ,relief = RIDGE )
148+ result .grid (row = 5 ,sticky = "w" ,padx = 15 ,pady = 5 )
149+
150+ modulus = Button (root ,text = "%" ,width = 2 ,command = modulus ,borderwidth = 3 ,relief = RIDGE )
151+ modulus .grid (row = 5 ,sticky = "e" ,padx = 125 ,pady = 5 )
152+
153+ root .mainloop ()
154+
155+ # <============ end code ==============>
0 commit comments