-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatorGUI.py
424 lines (367 loc) · 9.06 KB
/
calculatorGUI.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#Calculator Project Assignment for Y1-TA @ MEET Summer 2020.
import turtle
#Answers List.
Answ_List = []
#Calculator Screen.
SIZE_X=500
SIZE_Y=500
turtle.setup(SIZE_X, SIZE_Y)
turtle.tracer(1,0)
t = turtle.Turtle()
tDel = t.clone()
t.hideturtle()
tDel.hideturtle()
t.left(90)
############################################################################################
#Drawing & Defining GUI Buttons.
#btn0
t.penup()
t.goto(-240,-240)
t.pendown()
t.goto(-240,-200)
t.goto(-177.5,-200)
t.goto(-177.5,-240)
t.goto(-240,-240)
t.penup()
t.goto(-220,-245)
t.write("0", font=('Courier', 30, 'bold'))
#btn1
t.penup()
t.goto(-240,-190)
t.pendown()
t.goto(-240,-150)
t.goto(-177.5,-150)
t.goto(-177.5,-190)
t.goto(-240,-190)
t.penup()
t.goto(-220,-195)
t.write("1", font=('Courier', 30, 'bold'))
#btn4
t.penup()
t.goto(-240,-140)
t.pendown()
t.goto(-240,-100)
t.goto(-177.5,-100)
t.goto(-177.5,-140)
t.goto(-240,-140)
t.penup()
t.goto(-220,-145)
t.write("4", font=('Courier', 30, 'bold'))
#btn7
t.penup()
t.goto(-240,-90)
t.pendown()
t.goto(-240,-50)
t.goto(-177.5,-50)
t.goto(-177.5,-90)
t.goto(-240,-90)
t.penup()
t.goto(-220,-95)
t.write("7", font=('Courier', 30, 'bold'))
#btn=
t.penup()
t.goto(-167.5,-240)
t.pendown()
t.goto(-167.5,-200)
t.goto(-105,-200)
t.goto(-105,-240)
t.goto(-167.5,-240)
t.penup()
t.goto(-147.5,-245)
t.write("=", font=('Courier', 30, 'bold'))
#btn2
t.penup()
t.goto(-167.5,-190)
t.pendown()
t.goto(-167.5,-150)
t.goto(-105,-150)
t.goto(-105,-190)
t.goto(-167.5,-190)
t.penup()
t.goto(-147.5,-195)
t.write("2", font=('Courier', 30, 'bold'))
#btn5
t.penup()
t.goto(-167.5,-140)
t.pendown()
t.goto(-167.5,-100)
t.goto(-105,-100)
t.goto(-105,-140)
t.goto(-167.5,-140)
t.penup()
t.goto(-147.5,-145)
t.write("5", font=('Courier', 30, 'bold'))
#btn8
t.penup()
t.goto(-167.5,-90)
t.pendown()
t.goto(-167.5,-50)
t.goto(-105,-50)
t.goto(-105,-90)
t.goto(-167.5,-90)
t.penup()
t.goto(-147.5,-95)
t.write("8", font=('Courier', 30, 'bold'))
#btn+
t.penup()
t.goto(-95,-240)
t.pendown()
t.goto(-95,-200)
t.goto(-32.5,-200)
t.goto(-32.5,-240)
t.goto(-95,-240)
t.penup()
t.goto(-75,-245)
t.write("+", font=('Courier', 30, 'bold'))
#btn3
t.penup()
t.goto(-95,-190)
t.pendown()
t.goto(-95,-150)
t.goto(-32.5,-150)
t.goto(-32.5,-190)
t.goto(-95,-190)
t.penup()
t.goto(-75,-195)
t.write("3", font=('Courier', 30, 'bold'))
#btn6
t.penup()
t.goto(-95,-140)
t.pendown()
t.goto(-95,-100)
t.goto(-32.5,-100)
t.goto(-32.5,-140)
t.goto(-95,-140)
t.penup()
t.goto(-75,-145)
t.write("6", font=('Courier', 30, 'bold'))
#btn9
t.penup()
t.goto(-95,-90)
t.pendown()
t.goto(-95,-50)
t.goto(-32.5,-50)
t.goto(-32.5,-90)
t.goto(-95,-90)
t.penup()
t.goto(-75,-95)
t.write("9", font=('Courier', 30, 'bold'))
#btn-
t.penup()
t.goto(-22.5,-240)
t.pendown()
t.goto(-22.5,-200)
t.goto(40,-200)
t.goto(40,-240)
t.goto(-22.5,-240)
t.penup()
t.goto(-0.5,-245)
t.write("-", font=('Courier', 30, 'bold'))
#btn*
t.penup()
t.goto(-22.5,-190)
t.pendown()
t.goto(-22.5,-150)
t.goto(40,-150)
t.goto(40,-190)
t.goto(-22.5,-190)
t.penup()
t.goto(-2.5,-202.5)
t.write("*", font=('Courier', 35, 'bold'))
#btn/
t.penup()
t.goto(-22.5,-140)
t.pendown()
t.goto(-22.5,-100)
t.goto(40,-100)
t.goto(40,-140)
t.goto(-22.5,-140)
t.penup()
t.goto(-0.5,-140)
t.write("/", font=('Courier', 25, 'bold'))
#btn^
t.penup()
t.goto(-22.5,-90)
t.pendown()
t.goto(-22.5,-50)
t.goto(40,-50)
t.goto(40,-90)
t.goto(-22.5,-90)
t.penup()
t.goto(-0.5,-95)
t.write("^", font=('Courier', 30, 'bold'))
############################################################################################
#Calculator Answer Screen.
t.goto(-240,-40)
t.pendown()
t.goto(-240,240)
t.goto(40,240)
t.goto(40,-40)
t.goto(-240,-40)
t.penup()
#Calculator Answer Screen Delete.
def delete():
tDel.penup()
tDel.fillcolor("white")
tDel.begin_fill()
tDel.goto(-235,-35)
tDel.goto(-235,235)
tDel.goto(35,235)
tDel.goto(35,-35)
tDel.goto(-235,-35)
tDel.end_fill()
#Calculator History.
t.penup()
t.goto(50,250)
t.pendown()
t.goto(50,-250)
t.penup()
t.goto(90,215)
t.write("History", font=('Courier', 20, 'bold'))
t.goto(250,210)
t.pendown()
t.goto(50,210)
t.penup()
t.goto(60,180)
#Clear Calculator History.
def btnclear():
t.penup()
t.goto(240,-230)
t.pendown()
t.goto(180,-230)
t.goto(180,-210)
t.goto(240,-210)
t.goto(240,-230)
t.penup()
t.goto(185,-230)
t.write("Clear", font=("Courier", 12, 'bold'))
t.goto(60,180)
def clear(x,y):
if x > 180 and x < 240 and y > -230 and y < -210:
t.fillcolor("white")
t.begin_fill()
t.goto(55,210)
t.goto(250,210)
t.goto(250,-250)
t.goto(55,-250)
t.goto(55,210)
t.end_fill()
t.goto(50,210)
t.pendown()
t.goto(250,210)
t.penup()
btnclear()
print("\nCleared History! Press Enter To Continue")
turtle.onscreenclick(clear, 1)
turtle.listen
btnclear()
#Defining The Calculator Operations And Their Functions.
def add(x,y):
return x + y
def subtract(x,y):
return x - y
def multiply(x,y):
return x * y
def divide(x,y):
return x / y
def power(x,y):
return x ** y
#Welcome & Instructions Text.
print("Welcome To George's Helpful Calculator!\nThe Operations That Are Available Are:")
print("1.Addition(+)\n2.Subtraction(-)\n3.Multiplication(*)\n4.Division(/)\n5.Exponent(^)")
input("\nPress Enter to Start")
#Defining The Calculation Function, Asking For The User's Input & Error Handling.
def calculate():
try:
delete()
x = float(input("\nEnter Your First Number: "))
tDel.penup()
tDel.goto(-200,200)
tDel.write(x, font=('Courier', 13, 'bold'))
y = float(input("Enter Your Second Number: "))
tDel.goto(-200,150)
tDel.write(y, font=('Courier', 13, 'bold'))
except ValueError:
print("ERROR 'Integers Only'")
calculate()
else:
try:
operation = input("Enter Your Operation Of Choice: ")
tDel.goto(-200,175)
tDel.write(operation, font=('Courier', 13, 'bold'))
if operation in ('+', '-', '*', '/', '^'):
if operation == '+':
Answ1 = add(x, y)
print("Answer:", x, "+", y, "=", Answ1)
tDel.goto(-200,125)
tDel.write("=", font=('Courier', 13, 'bold'))
tDel.goto(-200,100)
tDel.write(Answ1, font=('Courier', 13, 'bold'))
Answ_List.append(Answ1)
hlp()
elif operation == '-':
Answ2 = subtract(x, y)
print("Answer:", x, "-", y, "=", Answ2)
tDel.goto(-200,125)
tDel.write("=", font=('Courier', 13, 'bold'))
tDel.goto(-200,100)
tDel.write(Answ2, font=('Courier', 13, 'bold'))
Answ_List.append(Answ2)
hlp()
elif operation == '*':
Answ3 = multiply(x, y)
print("Answer:", x, "*", y, "=", Answ3)
tDel.goto(-200,125)
tDel.write("=", font=('Courier', 13, 'bold'))
tDel.goto(-200,100)
tDel.write(Answ3, font=('Courier', 13, 'bold'))
Answ_List.append(Answ3)
hlp()
elif operation == '/':
Answ4 = divide(x, y)
print("Answer:", x, "/", y, "=", Answ4)
tDel.goto(-200,125)
tDel.write("=", font=('Courier', 13, 'bold'))
tDel.goto(-200,100)
tDel.write(Answ4, font=('Courier', 13, 'bold'))
Answ_List.append(Answ4)
hlp()
elif operation == '^':
Answ5 = power(x, y)
print("Answer:", x, "^", y, "=", Answ5)
tDel.goto(-200,125)
tDel.write("=", font=('Courier', 13, 'bold'))
tDel.goto(-200,100)
tDel.write(Answ5, font=('Courier', 13, 'bold'))
Answ_List.append(Answ5)
hlp()
else:
print("ERROR 'Invalid Operation'")
calculate()
except ZeroDivisionError:
print("ERROR 'Can Not Divide By Zero'")
calculate()
except OverflowError:
print("ERROR 'The Number Is Too Big For The Computer To Handle'")
calculate()
else:
rerun()
#Asking If The User Wants To Continue Or Stop Using The Calculator.
def rerun():
run_again = input("\nWould you like to calculate again?\nPlease Type Y for Yes or N for No: ")
if run_again.upper() == "Y":
calculate()
elif run_again.upper() == "N":
print("Good Luck!")
quit()
else:
rerun()
#History List Print.
def hlp():
for i in range(len(Answ_List)):
##print(Answ_List[i])
t.write(Answ_List[i], font=('Courier', 20, 'bold'))
Answ_List.clear()
t.backward(25)
calculate()
t.mainloop()