-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
285 lines (254 loc) · 6.59 KB
/
Calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from tkinter import *
# Let's create the Tkinter window
window = Tk()
# Then, you will define the size of the window in width(312) and height(324) using the 'geometry' method
window.geometry("312x324")
# In order to prevent the window from getting resized you will call 'resizable' method on the window
window.resizable(0, 0)
# Finally, define the title of the window
window.title("Calcualtor")
# Let's now define the required functions for the Calculator to function properly.
# 1. First is the button click 'btn_click' function which will continuously update the input field whenever a number is entered or any button is pressed it will act as a button click update.
def btn_click(item):
global expression
expression = expression + str(item)
input_text.set(expression)
# 2. Second is the button clear 'btn_clear' function clears the input field or previous calculations using the button "C"
def btn_clear():
global expression
expression = ""
input_text.set("")
# 3. Third and the final function is button equal ("=") 'btn_equal' function which will calculate the expression present in input field. For example: User clicks button 2, + and 3 then clicks "=" will result in an output 5.
def btn_equal():
global expression
result = str(
eval(expression)
) # 'eval' function is used for evaluating the string expressions directly
# you can also implement your own function to evalute the expression istead of 'eval' function
input_text.set(result)
expression = ""
expression = ""
# In order to get the instance of the input field 'StringVar()' is used
input_text = StringVar()
# Once all the functions are defined then comes the main section where you will start defining the structure of the calculator inside the GUI.
# The first thing is to create a frame for the input field
input_frame = Frame(
window,
width=312,
height=50,
bd=0,
highlightbackground="black",
highlightcolor="black",
highlightthickness=1,
)
input_frame.pack(side=TOP)
# Then you will create an input field inside the 'Frame' that was created in the previous step. Here the digits or the output will be displayed as 'right' aligned
input_field = Entry(
input_frame,
font=("arial", 18, "bold"),
textvariable=input_text,
width=50,
bg="#eee",
bd=0,
justify=RIGHT,
)
input_field.grid(row=0, column=0)
input_field.pack(
ipady=10
) # 'ipady' is an internal padding to increase the height of input field
# Once you have the input field defined then you need a separate frame which will incorporate all the buttons inside it below the 'input field'
btns_frame = Frame(window, width=312, height=272.5, bg="grey")
btns_frame.pack()
# The first row will comprise of the buttons 'Clear (C)' and 'Divide (/)'
clear = Button(
btns_frame,
text="C",
fg="black",
width=32,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_clear(),
).grid(row=0, column=0, columnspan=3, padx=1, pady=1)
divide = Button(
btns_frame,
text="/",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_click("/"),
).grid(row=0, column=3, padx=1, pady=1)
# The second row will comprise of the buttons '7', '8', '9' and 'Multiply (*)'
seven = Button(
btns_frame,
text="7",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(7),
).grid(row=1, column=0, padx=1, pady=1)
eight = Button(
btns_frame,
text="8",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(8),
).grid(row=1, column=1, padx=1, pady=1)
nine = Button(
btns_frame,
text="9",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(9),
).grid(row=1, column=2, padx=1, pady=1)
multiply = Button(
btns_frame,
text="*",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_click("*"),
).grid(row=1, column=3, padx=1, pady=1)
# The third row will comprise of the buttons '4', '5', '6' and 'Subtract (-)'
four = Button(
btns_frame,
text="4",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(4),
).grid(row=2, column=0, padx=1, pady=1)
five = Button(
btns_frame,
text="5",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(5),
).grid(row=2, column=1, padx=1, pady=1)
six = Button(
btns_frame,
text="6",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(6),
).grid(row=2, column=2, padx=1, pady=1)
minus = Button(
btns_frame,
text="-",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_click("-"),
).grid(row=2, column=3, padx=1, pady=1)
# The fourth row will comprise of the buttons '1', '2', '3' and 'Addition (+)'
one = Button(
btns_frame,
text="1",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(1),
).grid(row=3, column=0, padx=1, pady=1)
two = Button(
btns_frame,
text="2",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(2),
).grid(row=3, column=1, padx=1, pady=1)
three = Button(
btns_frame,
text="3",
fg="black",
width=10,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(3),
).grid(row=3, column=2, padx=1, pady=1)
plus = Button(
btns_frame,
text="+",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_click("+"),
).grid(row=3, column=3, padx=1, pady=1)
# Finally, the fifth row will comprise of the buttons '0', 'Decimal (.)', and 'Equal To (=)'
zero = Button(
btns_frame,
text="0",
fg="black",
width=21,
height=3,
bd=0,
bg="#fff",
cursor="hand2",
command=lambda: btn_click(0),
).grid(row=4, column=0, columnspan=2, padx=1, pady=1)
point = Button(
btns_frame,
text=".",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_click("."),
).grid(row=4, column=2, padx=1, pady=1)
equals = Button(
btns_frame,
text="=",
fg="black",
width=10,
height=3,
bd=0,
bg="#eee",
cursor="hand2",
command=lambda: btn_equal(),
).grid(row=4, column=3, padx=1, pady=1)
window.mainloop()