-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
52 lines (47 loc) · 1.7 KB
/
calc.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
# coding=utf-8
from Tkinter import *
def userinput(text): #按键返回函数
content = display.get() + text
display.set(content)
def clear(): #清空函数
display.set('')
def dell(): #删除一字符函数
display.set(str(display.get()[:-1]))
def calculate(): #调用eval计算表达式
try:
num=display.get()
res=eval(num)
display.set(num+'='+str(res))
except:
display.set('error!')
def main():
root = Tk()
root.title('计算器-yh')
global display
display = StringVar()
root.geometry('275x215+200+200')
root.resizable(0,0)
label = Label(root,relief='sunken',borderwidth=3, anchor=SE)#显示窗口
label['textvariable'] = display
label.config(bg='white', width=38, height=2)
label.grid(row=0, column=0, columnspan=4,pady=10)
positions=[(x,y) for x in range(1,6) for y in range(4)]
tests=['Cls','Bck','(',')',
'7','8','9','/',
'4','5','6','*',
'1','2','3','-',
'0','.','=','+']
for i in zip(tests,positions): #将两个列表并列
text=i[0]
row=i[1][0]
column=i[1][1]
if text=='Cls':
Button(root, text=text, width=8, command=lambda text=text: clear()).grid(row=row, column=column)
elif text=='Bck':
Button(root, text=text, width=8, command=lambda text=text: dell()).grid(row=row, column=column)
elif text=='=':
Button(root, text=text, bg='green', width=8, command=lambda text=text: calculate()).grid(row=row, column=column)
else:
Button(root, text=text, width=8, command=lambda text=text: userinput(text)).grid(row=row, column=column)
root.mainloop()
main()