Python制作一个简易计算器
1 import tkinter
idc=tkinter.Tk()
2 idc.title("计算器")
3 #记录算式
4 expstr=""
5 #记录运算历史
6 history=[]
7
8 def onClick(key):
9 global expstr
10
11 if key=="=":
12 jieguo =round(eval(expstr),2)
13 result["text"]=jieguo
14 frame_right.pack()
15
16 t=tkinter.Label(frame_inner,text=expstr+"="+str(jieguo),bg="seashell")
17 t.pack()
18 history.append(t)
19 elif key=="AC":
20 result["text"]=""
21 expstr=""
22 else:
23 expstr=expstr+str(key)
24 result["text"]=expstr
25
26
27 frame_grap=tkinter.Frame(idc)
28 frame_grap.pack(fill="y",side="left")
29 frame_left=tkinter.Frame(idc)
30
31 result=tkinter.Label(frame_left,bg="seashell",text="0",height=2,font=("Arial",34,"bold"))
32 result.grid(row=0,column=0,columnspan=4,sticky=tkinter.E)
33
34 ac=tkinter.Button(frame_left,text="AC",width=6,height=2,command=lambda:onClick("AC"))
35 ac.grid(row=1,column=0)
36
37 negative=tkinter.Button(frame_left,text="+/-",width=6,height=2,command=lambda:onClick("-"))
38 negative.grid(row=1,column=1)
39
40 percent=tkinter.Button(frame_left,text="%",width=6,height=2,command=lambda:onClick("/100"))
41 percent.grid(row=1,column=2)
42
43 division=tkinter.Button(frame_left,text="/",width=6,height=2,command=lambda:onClick("/"))
44 division.grid(row=1,column=3)
45
46 num7=tkinter.Button(frame_left,text="7",width=6,height=2,command=lambda:onClick(7))
47 num7.grid(row=2,column=0)
48 num8=tkinter.Button(frame_left,text="8",width=6,height=2,command=lambda:onClick(8))
49 num8.grid(row=2,column=1)
50 num9=tkinter.Button(frame_left,text="9",width=6,height=2,command=lambda:onClick(9))
51 num9.grid(row=2,column=2)
52 multi=tkinter.Button(frame_left,text="*",width=6,height=2,command=lambda:onClick("*"))
53 multi.grid(row=2,column=3)
54
55 num4=tkinter.Button(frame_left,text="4",width=6,height=2,command=lambda:onClick(4))
56 num4.grid(row=3,column=0)
57 num5=tkinter.Button(frame_left,text="5",width=6,height=2,command=lambda:onClick(5))
58 num5.grid(row=3,column=1)
59 num6=tkinter.Button(frame_left,text="6",width=6,height=2,command=lambda:onClick(6))
60 num6.grid(row=3,column=2)
61 sub=tkinter.Button(frame_left,text="-",width=6,height=2,command=lambda:onClick("-"))
62 sub.grid(row=3,column=3)
63
64 num1=tkinter.Button(frame_left,text="1",width=6,height=2,command=lambda:onClick(1))
65 num1.grid(row=4,column=0)
66 num2=tkinter.Button(frame_left,text="2",width=6,height=2,command=lambda:onClick(2))
67 num2.grid(row=4,column=1)
68 num3=tkinter.Button(frame_left,text="3",width=6,height=2,command=lambda:onClick(3))
69 num3.grid(row=4,column=2)
70 add=tkinter.Button(frame_left,text="+",width=6,height=2,command=lambda:onClick("+"))
71 add.grid(row=4,column=3)
72
73 num0=tkinter.Button(frame_left,text="0",width=6,height=2,command=lambda:onClick(0))
74 num0.grid(row=5,column=0,columnspan=2)
75 point=tkinter.Button(frame_left,text=".",width=6,height=2,command=lambda:onClick("."))
76 point.grid(row=5,column=2)
77 equals=tkinter.Button(frame_left,text="=",width=6,height=2,command=lambda:onClick("="))
78 equals.grid(row=5,column=3)
79
80 frame_left.pack(fill="y",side="left")
81 frame_right=tkinter.Frame(idc,width=200)
82 tkinter.Label(frame_right,text="运算历史",font=("Arial",14,"underline bold")).pack()
83 frame_inner=tkinter.Frame(frame_right)
84 frame_inner.pack(fill="x",side="top")
85
86 def clean_history():
87 for x in history:
88 print(x)
89 x.destroy()
90
91 cls_button=tkinter.Button(frame_right,text="清空",command=lambda:clean_history())
92 cls_button.pack(fill="x",side="top")
93
94 idc.mainloop()