-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython_calculator.py
More file actions
429 lines (385 loc) · 17 KB
/
Python_calculator.py
File metadata and controls
429 lines (385 loc) · 17 KB
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
425
426
427
428
429
#Special Calculator
#wrote by HAO PENG. ID:1780718
from tkinter import*
from math import*
num_reg=0 #fot one-argument option, this variable store the number. for two-arguments option, it store the first argument
symbol_record='' #this variable store the operational character
numtype_record=1 #if the vale of this variable is 0, it represents integer mode. if it is 1, it represent float mode
angtype_record=0 #if the vale of this variable is 0, it represents degree mode. if it is 1, it represent radian mode
def clean(event): #clean the entry text and initialize all variables
global symbol_record
global num_reg
num_reg=0
symbol_record=''
display['text']=''
numEntry.delete(0,'end')
def delete(event): #delect function
global num_reg
global symbol_record
ind=numEntry.index('end')
ind=ind-1
if symbol_record=='': #if there is no operational character, then it will delete last digit of num_reg
numEntry.delete(ind)
if ind>0:
#if numtype_record==0:
num_reg=numEntry.get()
#else:
#num_reg=float(numEntry.get())
numEntry.delete(0,'end')
numEntry.insert('end',num_reg)
if ind==0:
num_reg=0
else: #if there is operational character, then it will delete last digit of second argument
if ind>0:
numEntry.delete(ind)
#if numtype_record==0:
num=numEntry.get()
#else:
#num=float(numEntry.get())
numEntry.delete(0,'end')
numEntry.insert('end',num)
if ind==0:
numEntry.delete(ind)
if ind<0: #or delect the operator
symbol_record=''
numEntry.delete(0,'end')
numEntry.insert('end',num_reg)
display['text']=str(num_reg)+symbol_record
def integers(event): #set mode as integer mode
global numtype_record
numtype_record=0
label1['text']='Integer mood'
def floats(event): #set mode as float mode
global numtype_record
numtype_record=1
label1['text']='Float mood'
def degrees(event): #set mode as degree mode
global angtype_record
angtype_record=0
label2['text']='Degree mood'
def radians(event): #set mode as radian mode
global angtype_record
angtype_record=1
label2['text']='Radian mood'
def seven(event): #input 7
numEntry.insert('end',7)
def eight(event): #input 8
numEntry.insert('end',8)
def nine(event): #input 9
numEntry.insert('end',9)
def four(event): #input 4
numEntry.insert('end',4)
def five(event): #input 5
numEntry.insert('end',5)
def six(event): #input 6
numEntry.insert('end',6)
def one(event): #input 1
numEntry.insert('end',1)
def two(event): #input 2
numEntry.insert('end',2)
def three(event): #input 3
numEntry.insert('end',3)
def zero(event): #input 0
numEntry.insert('end',0)
def point(event): #input '.'
numEntry.insert('end','.')
def subtract(event): #funvtion of subtraction and negative indication
global num_reg
global symbol_record
global numtype_record
ind=numEntry.index('end')
if ind==0: #if there is no value,the '-' will represent negative indication
numEntry.insert('end','-')
else: #if there has value,the '-' will represent subtraction
if numtype_record==0:
num_reg=int(numEntry.get())
else:
num_reg=float(numEntry.get())
display['text']=str(num_reg)+'-'
numEntry.delete(0,'end')
symbol_record='-'
def add(event): #funvtion of addition
global num_reg
global symbol_record
global numtype_record
ind=numEntry.index('end')
if ind==0:
numEntry.insert('end','+')
else:
if numtype_record==0:
num_reg=int(numEntry.get())
else:
num_reg=float(numEntry.get())
display['text']=str(num_reg)+'+'
numEntry.delete(0,'end')
symbol_record='+'
def mul(event): #funvtion of multipling
global num_reg
global symbol_record
global numtype_record
if numtype_record==0:
num_reg=int(numEntry.get())
else:
num_reg=float(numEntry.get())
display['text']=str(num_reg)+'*'
numEntry.delete(0,'end')
symbol_record='*'
def div(event): #funvtion of dividing
global num_reg
global symbol_record
global numtype_record
if numtype_record==0:
num_reg=int(numEntry.get())
else:
num_reg=float(numEntry.get())
display['text']=str(num_reg)+'/'
numEntry.delete(0,'end')
symbol_record='/'
def square_root(event): #Founction of square_root
global numtype_record
global num_reg
num=float(numEntry.get())
if num<0:
display['text']='√'+str(num)+' negative input'
else:
if numtype_record==0:
num_reg=int(sqrt(num))
num=int(num)
else:
num_reg=float(sqrt(num))
display['text']='√'+str(num)+'='+str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
def raise_to_power(event): #Founction of power
global num_reg
global symbol_record
global numtype_record
if numtype_record==0:
num_reg=int(numEntry.get())
else:
num_reg=float(numEntry.get())
display['text']=str(num_reg)+'^'
numEntry.delete(0,'end')
symbol_record='^'
def log_ten(event): #Founction of log based on 10
global numtype_record
num=float(numEntry.get())
if num<=0:
display['text']='log'+str(num)+' input is unvaluable, error'
else:
if numtype_record==0:
num_reg=int(log10(num))
num=int(num)
else:
num_reg=float(log10(num))
display['text']='log'+str(num)+'='+str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
def sins(event): #Founction of sin
global numtype_record
global angtype_record
num=float(numEntry.get())
if angtype_record==0:
if numtype_record==0:
num_reg=int(sin(num*pi/180)) #in degree mode, it will transform degree to radian, then caculate the function
num=int(num)
else:
num_reg=float(sin(num*pi/180))
else:
if numtype_record==0:
num_reg=int(sin(num))
num=int(num)
else:
num_reg=float(sin(num))
display['text']='sin'+str(num)+'='+str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
def coss(event): #Founction of cos
global numtype_record
global angtype_record
num=float(numEntry.get())
if angtype_record==0:
if numtype_record==0:
num_reg=int(cos(num*pi/180))
num=int(num)
else:
num_reg=float(cos(num*pi/180))
else:
if numtype_record==0:
num_reg=int(cos(num))
num=int(num)
else:
num_reg=float(cos(num))
display['text']='cos'+str(num)+'='+str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
def tans(event): #Founction of tans
global numtype_record
global angtype_record
num=float(numEntry.get())
if angtype_record==0:
if numtype_record==0:
num_reg=int(tan(num*pi/180))
num=int(num)
else:
num_reg=float(tan(num*pi/180))
else:
if numtype_record==0:
num_reg=int(tan(num))
num=int(num)
else:
num_reg=float(tan(num))
display['text']='tan'+str(num)+'='+str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
def equal(event): #Founction of equality sign
global num_reg
global symbol_record
global numtype_record
if numtype_record==0:
num=int(numEntry.get())
else:
num=float(numEntry.get())
num2=num_reg
if symbol_record=='^': #for two-arguments operation, the function 'equal' will distinguish what operator has been inputed, then run different function. here it runs power function
if numtype_record==0:
num_reg=int(pow(num_reg,num))
else:
num_reg=float(pow(num_reg,num))
display['text']=str(num2)+'^'+str(num)+'='+str(num_reg)
if symbol_record=='-': #here is subtraction
num_reg=num_reg-num
display['text']=str(num2)+'-'+str(num)+'='+str(num_reg)
if symbol_record=='+': #here is addition
num_reg=num_reg+num
display['text']=str(num2)+'+'+str(num)+'='+str(num_reg)
if symbol_record=='*': #here is multipling
num_reg=num_reg*num
display['text']=str(num2)+'*'+str(num)+'='+str(num_reg)
if symbol_record=='/': #here is dividing
if num==0:
display['text']=str(num2)+'/'+str(num)+' dividend is 0, error'
else:
if numtype_record==0:
num_reg=int(num_reg/num)
else:
num_reg=float(num_reg/num)
display['text']=str(num2)+'/'+str(num)+'='+str(num_reg)
if symbol_record=='': #here is just equal for one argument
num_reg=num
display['text']=str(num_reg)
numEntry.delete(0,'end')
numEntry.insert(0,num_reg)
symbol_record=''
root=Tk() #definition of GUI interface
root.title('Special Calculatro')
root.geometry("350x230+200+20")
frame0=Frame(root,bg='black',width=350) #I has definded 4 frame. the first is used to displaying. the second is used to select mode. the third is use to input and output. the last one is used to construct function buttons.
frame0.grid(row=0,column=0,sticky=W,padx=0,pady=0,ipadx=50)
display=Label(frame0,text='',anchor='nw',bg='black',fg='white',width=35,justify='left')
display.pack(fill=X)
frame1=Frame(root,bg='black',width=350)
frame1.grid(row=1,column=0,sticky=W,padx=0,pady=0,ipadx=3)
label1=Label(frame1,text='Float mood',bg='#272727',fg='white',anchor='center',justify='center',width=12) # display laber
label1.grid(row=0,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=40)
label2=Label(frame1,text='Degree mood',bg='#272727',fg='white',anchor='center',justify='center',width=12)
label2.grid(row=0,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=40)
frame2=Frame(root,bg="black",width=350)
frame2.grid(row=2,column=0,sticky=W,padx=0,pady=0,ipadx=2)
intButton=Button(frame2,text='Integer',width=8,bg='#adadad',fg='white') # mode select button
intButton.bind("<Button-1>",integers)
intButton.grid(row=0,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=11)
floButton=Button(frame2,text='Float',width=7,bg='#adadad',fg='white')
floButton.bind("<Button-1>",floats)
floButton.grid(row=0,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=11)
degButton=Button(frame2,text='Degree',width=8,bg='#adadad',fg='white')
degButton.bind("<Button-1>",degrees)
degButton.grid(row=0,column=2,sticky=W+E+N+S,padx=1,pady=2,ipadx=11)
radButton=Button(frame2,text='Radian',width=7,bg='#adadad',fg='white')
radButton.bind("<Button-1>",radians)
radButton.grid(row=0,column=3,sticky=W+E+N+S,padx=1,pady=2,ipadx=11)
frame3=Frame(root,bg='black',width=350)
frame3.grid(row=4,column=0,sticky=W,padx=0,pady=0,ipadx=3)
numEntry=Entry(frame3,bg='black',fg='white') #entry type which achieve the inputing and outputing of number
numEntry.pack(side=LEFT,padx=1,pady=2,ipadx=40)
DButton=Button(frame3,text='Delete',fg='white',bg='red') #delect button
DButton.bind("<Button-1>",delete)
DButton.pack(side=LEFT,padx=1,pady=2,ipadx=13)
cButton=Button(frame3,text='Clear',fg='white',bg='orange') #clean button
cButton.bind("<Button-1>",clean)
cButton.pack(side=LEFT,padx=1,pady=2,ipadx=13)
frame4=Frame(root,bg="black",width=350) #in this frame, all the elements using grid to construct.
frame4.grid(row=5,column=0,sticky=W,padx=0,pady=0,ipadx=4)
sevenButton=Button(frame4,text='7',bg='#3c3c3c',fg='white',width=3) #here are numbers button for 0 to 9
sevenButton.bind("<Button-1>",seven)
sevenButton.grid(row=0,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
eightButton=Button(frame4,text='8',bg='#3c3c3c',fg='white',width=3)
eightButton.bind("<Button-1>",eight)
eightButton.grid(row=0,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
nineButton=Button(frame4,text='9',bg='#3c3c3c',fg='white',width=3)
nineButton.bind("<Button-1>",nine)
nineButton.grid(row=0,column=2,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
fourButton=Button(frame4,text='4',bg='#3c3c3c',fg='white',width=3)
fourButton.bind("<Button-1>",four)
fourButton.grid(row=1,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
fiveButton=Button(frame4,text='5',bg='#3c3c3c',fg='white',width=3)
fiveButton.bind("<Button-1>",five)
fiveButton.grid(row=1,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
sixButton=Button(frame4,text='6',bg='#3c3c3c',fg='white',width=3)
sixButton.bind("<Button-1>",six)
sixButton.grid(row=1,column=2,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
oneButton=Button(frame4,text='1',bg='#3c3c3c',fg='white',width=3)
oneButton.bind("<Button-1>",one)
oneButton.grid(row=2,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
twoButton=Button(frame4,text='2',bg='#3c3c3c',fg='white',width=3)
twoButton.bind("<Button-1>",two)
twoButton.grid(row=2,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
threeButton=Button(frame4,text='3',bg='#3c3c3c',fg='white',width=3)
threeButton.bind("<Button-1>",three)
threeButton.grid(row=2,column=2,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
addButton=Button(frame4,text='+',bg='orange',fg='black',width=3) #'+'button
addButton.bind("<Button-1>",add)
addButton.grid(row=2,column=3,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
mulButton=Button(frame4,text='*',bg='orange',fg='black',width=3) #'*'button
mulButton.bind("<Button-1>",mul)
mulButton.grid(row=2,column=4,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
name1=Label(frame4,text='Designer:',bg='black',fg='white',anchor='center',justify='center',width=3)
name1.grid(row=2,column=5,sticky=W+E+N+S,padx=1,pady=2,ipadx=4)
zeroButton=Button(frame4,text='0',bg='#3c3c3c',fg='white',width=3)
zeroButton.bind("<Button-1>",zero)
zeroButton.grid(row=3,column=0,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
pointButton=Button(frame4,text='.',bg='#3c3c3c',fg='white',width=3) #here is '.' button
pointButton.bind("<Button-1>",point)
pointButton.grid(row=3,column=1,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
equButton=Button(frame4,text='=',width=3) # here is "euqal" button
equButton.bind("<Button-1>",equal)
equButton.grid(row=3,column=2,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
subButton=Button(frame4,text='-',bg='orange',fg='black') #'-'button
subButton.bind("<Button-1>",subtract)
subButton.grid(row=3,column=3,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
divButton=Button(frame4,text='/',bg='orange',fg='black') #'/'button
divButton.bind("<Button-1>",div)
divButton.grid(row=3,column=4,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
name2=Label(frame4,text='HAO',bg='black',fg='white',anchor='center',justify='center',width=3)
name2.grid(row=3,column=5,sticky=W+E+N+S,padx=1,pady=2,ipadx=4)
powButton=Button(frame4,text='√ sqr',bg='#7b7b7b',fg='white',width=3) #'√'button
powButton.bind("<Button-1>",square_root)
powButton.grid(row=0,column=3,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
powButton=Button(frame4,text='^ pow',bg='#7b7b7b',fg='white',width=3) #'^'button
powButton.bind("<Button-1>",raise_to_power)
powButton.grid(row=0,column=4,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
logButton=Button(frame4,text='log10',bg='#7b7b7b',fg='white',width=3) #'log'button
logButton.bind("<Button-1>",log_ten)
logButton.grid(row=0,column=5,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
sinButton=Button(frame4,text='sin',bg='#7b7b7b',fg='white',width=3) #'sin'button
sinButton.bind("<Button-1>",sins)
sinButton.grid(row=1,column=3,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
cosButton=Button(frame4,text='cos',bg='#7b7b7b',fg='white',width=3) #'cos'button
cosButton.bind("<Button-1>",coss)
cosButton.grid(row=1,column=4,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
tanButton=Button(frame4,text='tan',bg='#7b7b7b',fg='white',width=3) #'tan'button
tanButton.bind("<Button-1>",tans)
tanButton.grid(row=1,column=5,sticky=W+E+N+S,padx=1,pady=2,ipadx=12)
frame4=Frame(root,bg="black",width=350,height=10) #in this frame, all the elements using grid to construct.
frame4.grid(row=6,column=0,sticky=W,padx=0,pady=0,ipadx=0)
root.mainloop()