-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
412 lines (312 loc) · 9.49 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
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
import tkinter
from tkinter import *
from tkinter import messagebox
root = tkinter.Tk()
root.geometry("400x500+500+100")
root.resizable(0, 0)
root.title("CALCULATOR")
root.iconbitmap('cal_images/logo.ico')
root.wm_attributes('-alpha', '0.9')
val = ''
a = 0.0
b = 0.0
operator = ''
dot_ct = 0
neg_ct = 0
def div_x_clicked():
global val
global a
a = float(val)
a = 1/a
val = str(a)
data.set(val)
def sq_clicked():
global val
global a
a = float(val)
a = a * a
val = str(a)
data.set(val)
def neg_clicked():
global val
global a
global operator
global neg_ct
global dot_ct
a = float(val)
if a<0:
a = abs(a)
val = str(a)
else:
neg_ct = 1
val = '-' + str(a)
data.set(val)
dot_ct = 1
def sq_root_clicked():
global val
global a
a = float(val)
a = a ** (1 / 2)
val = str(a)
data.set(val)
#change the button.
def ce_clicked():
c_clicked()
def c_clicked():
global val
global a
global operator
global dot_ct
val = ''
operator = ''
a = 0
data.set(val)
dot_ct = 0
def del_clicked():
global val
global dot_ct
last = val[-1]
if last == '.':
dot_ct = 0
val = val[:-1]
data.set(val)
if last == '+' or last == '-' or last == 'x' or last == '/':
if val.find('.') != -1: #found
dot_ct = 1
elif val.find('.') == -1: #notFound
dot_ct = 0
def percent_clicked():
global dot_ct
global a
global val
global operator
val2 = val
dot_ct = 0
if operator=='x':
b = float(val2.split('x')[1])
ans = (a * b)/100
val = str(ans)
elif operator=='/':
b = float(val2.split('/')[1])
ans = (a / b) * 100
val = str(ans)
data.set(val)
def seven_clicked():
global val
val = val + '7'
data.set(val)
def eight_clicked():
global val
val = val + '8'
data.set(val)
def nine_clicked():
global val
val = val + '9'
data.set(val)
def div_clicked():
global val
global a
global operator
global dot_ct
operator = '/'
a = float(val)
val = val + '/'
data.set(val)
dot_ct = 0
def four_clicked():
global val
val = val + '4'
data.set(val)
def five_clicked():
global val
val = val + '5'
data.set(val)
def six_clicked():
global val
val = val + '6'
data.set(val)
def mul_clicked():
global val
global operator
global a
global dot_ct
a = float(val)
operator = 'x'
val = val + 'x'
data.set(val)
dot_ct = 0
def one_clicked():
global val
val = val + '1'
data.set(val)
def two_clicked():
global val
val = val + '2'
data.set(val)
def three_clicked():
global val
val = val + '3'
data.set(val)
def minus_clicked():
global val
global operator
global a
global dot_ct
a = float(val)
operator = '-'
val = val + '-'
data.set(val)
dot_ct = 0
def zero_clicked():
global val
val = val + '0'
data.set(val)
def dot_clicked():
global val
global dot_ct
if dot_ct == 0:
val = val + '.'
data.set(val)
dot_ct = 1
def plus_clicked():
global val
global operator
global a
global dot_ct
a = float(val)
operator = '+'
val = val + '+'
data.set(val)
dot_ct = 0
def result():
global val
global operator
global a
global b
global dot_ct
dot_ct =1
val2 = val
if operator == '+':
b = float(val2.split('+')[1])
ans = a + b
data.set(ans)
val = str(ans)
elif operator == '-':
if neg_ct==1:
b = float(val2.split('-')[2])
else:
b = float(val2.split('-')[1])
ans = a - b
data.set(ans)
val = str(ans)
elif operator == 'x':
b = float(val2.split('x')[1])
ans = a * b
data.set(ans)
val = str(ans)
elif operator == '/':
b = float(val2.split('/')[1])
if b == 0:
tkinter.messagebox.showerror('ERROR', 'Division Tends To Infinite')
else:
ans = a / b
data.set(ans)
val = str(ans)
def close_clicked():
close = tkinter.messagebox.askyesno("calculator", "Click YES if you want to exit")
if close == TRUE:
root.destroy()
return
menu = Menu(root)
root.config(menu=menu)
moremenu = Menu(menu)
moremenu = Menu(menu, tearoff=0)
moremenu.add_command(label='Exit', command=close_clicked)
data = StringVar()
lbl = Label(root, text='Label', anchor=SE, font=("Verdana", 40), textvariable=data, background='#1D1F1E', fg='#ffffff', )
lbl.pack(expand=True, fill="both", )
btnrow1 = Frame(root)
btnrow1.pack(expand=True, fill="both")
btnrow2 = Frame(root)
btnrow2.pack(expand=True, fill="both")
btnrow3 = Frame(root)
btnrow3.pack(expand=True, fill="both")
btnrow4 = Frame(root)
btnrow4.pack(expand=True, fill="both")
btnrow5 = Frame(root)
btnrow5.pack(expand=True, fill="both")
btnrow6 = Frame(root)
btnrow6.pack(expand=True, fill="both")
onePhoto = PhotoImage(file="cal_images/1.png")
twoPhoto = PhotoImage(file="cal_images/2.png")
threePhoto = PhotoImage(file="cal_images/3.png")
fourPhoto = PhotoImage(file="cal_images/4.png")
fivePhoto = PhotoImage(file="cal_images/5.png")
sixPhoto = PhotoImage(file="cal_images/6.png")
sevenPhoto = PhotoImage(file="cal_images/7.png")
eightPhoto = PhotoImage(file="cal_images/8.png")
ninePhoto = PhotoImage(file="cal_images/9.png")
zeroPhoto = PhotoImage(file="cal_images/zero.png")
dotPhoto = PhotoImage(file="cal_images/dot.png")
equalPhoto = PhotoImage(file="cal_images/equal.png")
plusPhoto = PhotoImage(file="cal_images/plus.png")
minusPhoto = PhotoImage(file="cal_images/minus.png")
multiplyPhoto = PhotoImage(file="cal_images/multiply.png")
dividePhoto = PhotoImage(file="cal_images/divide.png")
modulePhoto = PhotoImage(file="cal_images/module.png")
rootPhoto = PhotoImage(file="cal_images/root.png")
plusMinusPhoto = PhotoImage(file="cal_images/plusMinus.png")
squarePhoto = PhotoImage(file="cal_images/square.png")
delPhoto = PhotoImage(file="cal_images/delete.png")
cPhoto = PhotoImage(file="cal_images/c.png")
oneByXPhoto = PhotoImage(file="cal_images/oneByX.png")
cePhoto = PhotoImage(file="cal_images/ce.png")
btn1 = Button(btnrow1, image=oneByXPhoto, relief=GROOVE, border=0, command=div_x_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow1, image=squarePhoto, relief=GROOVE, border=0, command=sq_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow1, image=plusMinusPhoto, relief=GROOVE, border=0, command=neg_clicked)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow1, image=rootPhoto, relief=GROOVE, border=0, command=sq_root_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
btn1 = Button(btnrow2, image=cePhoto, relief=GROOVE, border=0, command=ce_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow2, image=cPhoto, relief=GROOVE, border=0, command=c_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow2, image = delPhoto, relief=GROOVE, border=0, command=del_clicked)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow2, image=modulePhoto, relief=GROOVE, border=0, command=percent_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
btn1 = Button(btnrow3, image=sevenPhoto, relief=GROOVE, border=0, command=seven_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow3, image=eightPhoto, relief=GROOVE, border=0, command=eight_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow3, image=ninePhoto, relief=GROOVE, border=0, command=nine_clicked)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow3, image=dividePhoto, relief=GROOVE, border=0, command=div_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
btn1 = Button(btnrow4, image=fourPhoto, relief=GROOVE, border=0, command=four_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow4, image=fivePhoto, relief=GROOVE, border=0, command=five_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow4, image=sixPhoto, relief=GROOVE, border=0, command=six_clicked)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow4, image=multiplyPhoto, relief=GROOVE, border=0, command=mul_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
btn1 = Button(btnrow5, image=onePhoto, relief=GROOVE, border=0, command=one_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow5, image=twoPhoto, relief=GROOVE, border=0, command=two_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow5, image=threePhoto, relief=GROOVE, border=0, command=three_clicked)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow5, image=minusPhoto, relief=GROOVE, border=0, command=minus_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
btn1 = Button(btnrow6, image=zeroPhoto, relief=GROOVE, border=0, command=zero_clicked)
btn1.pack(side=LEFT, expand=True, fill="both", )
btn2 = Button(btnrow6, image=dotPhoto, relief=GROOVE, border=0, command=dot_clicked)
btn2.pack(side=LEFT, expand=True, fill="both", )
btn3 = Button(btnrow6, image=equalPhoto, relief=GROOVE, border=0, command=result)
btn3.pack(side=LEFT, expand=True, fill="both", )
btn4 = Button(btnrow6, image=plusPhoto, relief=GROOVE, border=0, command=plus_clicked)
btn4.pack(side=LEFT, expand=True, fill="both", )
mainloop()