from tkinter import * root=Tk() root.title("计算器") root.geometry('350x547') #显示计算结果 slabel=StringVar() show=Entry(root,textvariable=slabel,bg="white",justify="right",font=("黑体",16),state="disabled") show.grid(row=0,rowspan=2,column=1,ipadx=15,ipady=60,columnspan=3,pady=3,padx=3) deng=Button(root,text="=",bg="lightBlue",font=("黑体",20,"bold")) deng.grid(row=0,column=4,ipadx=20,ipady=10,pady=3) dian=Button(root,text=".",bg="lightBlue",font=("黑体",20,"bold")) dian.grid(row=1,column=4,ipadx=20,ipady=10,pady=3) #1,2,3,+ y=Button(root,text="1",bg="lightBlue",font=("黑体",20,"bold")) y.grid(row=2,column=1,ipadx=20,ipady=20,padx=3,pady=3) e=Button(root,text="2",bg="lightBlue",font=("黑体",20,"bold")) e.grid(row=2,column=2,ipadx=20,ipady=20,padx=3,pady=3) san=Button(root,text="3",bg="lightBlue",font=("黑体",20,"bold")) san.grid(row=2,column=3,ipadx=20,ipady=20,padx=3,pady=3) jia=Button(root,text="+",bg="lightBlue",font=("黑体",20,"bold")) jia.grid(row=2,column=4,ipadx=20,ipady=20,padx=3,pady=3) #4,5,6,- s=Button(root,text="4",bg="lightBlue",font=("黑体",20,"bold")) s.grid(row=3,column=1,ipadx=20,ipady=20,padx=3,pady=3) w=Button(root,text="5",bg="lightBlue",font=("黑体",20,"bold")) w.grid(row=3,column=2,ipadx=20,ipady=20,padx=3,pady=3) l=Button(root,text="6",bg="lightBlue",font=("黑体",20,"bold")) l.grid(row=3,column=3,ipadx=20,ipady=20,padx=3,pady=3) jian=Button(root,text="-",bg="lightBlue",font=("黑体",20,"bold")) jian.grid(row=3,column=4,ipadx=20,ipady=20,padx=3,pady=3) #7,8,9,X q=Button(root,text="7",bg="lightBlue",font=("黑体",20,"bold")) q.grid(row=4,column=1,ipadx=20,ipady=20,padx=3,pady=3) b=Button(root,text="8",bg="lightBlue",font=("黑体",20,"bold")) b.grid(row=4,column=2,ipadx=20,ipady=20,padx=3,pady=3) j=Button(root,text="9",bg="lightBlue",font=("黑体",20,"bold")) j.grid(row=4,column=3,ipadx=20,ipady=20,padx=3,pady=3) cheng=Button(root,text="X",bg="lightBlue",font=("黑体",20,"bold")) cheng.grid(row=4,column=4,ipadx=20,ipady=20,padx=3,pady=3) #0,clear,/ z=Button(root,text="0",bg="lightBlue",font=("黑体",20,"bold")) z.grid(row=5,column=1,ipadx=20,ipady=20,padx=3,pady=3) sclear=Button(root,text="清空",bg="lightBlue",font=("黑体",12,"bold")) sclear.grid(row=5,column=2,ipadx=56,ipady=30,columnspan=2,padx=3,pady=3) chu=Button(root,text="/",bg="lightBlue",font=("黑体",20,"bold")) chu.grid(row=5,column=4,ipadx=20,ipady=20,padx=3,pady=3) #函数 slist=[] elist=[] unit="" def into(event): global unit slabel.set(show.get()+event.widget['text']) if event.widget['text'] in ["+","-","X","/"]: slist.append(unit) slist.append(event.widget['text']) unit="" else: unit=unit+event.widget['text'] def den(event): global unit global slist global elist slist.append(unit) unit="" for i in range(len(slist)): if(slist[i] in ["X"]): slist[i+1]=float(slist[i-1])*float(slist[i+1]) elist.pop(len(elist)-1) continue elif(slist[i] in ["/"]): slist[i+1]=float(slist[i-1])/float(slist[i+1]) elist.pop(len(elist)-1) continue else: elist.append(slist[i]) for j in range(len(elist)): if(elist[j] in ["+"]): elist[j+1]=float(elist[j-1])+float(elist[j+1]) j=j+1 elif(elist[j] in ["-"]): elist[j+1]=float(elist[j-1])-float(elist[j+1]) j=j+1 unit=round(elist[len(elist)-1],2) slabel.set(str(unit)) slist=[] elis=[] def clear(event): unit="" slabel.set(unit) #事件 y.bind('<Button-1>',into) e.bind('<Button-1>',into) san.bind('<Button-1>',into) jia.bind('<Button-1>',into) s.bind('<Button-1>',into) w.bind('<Button-1>',into) l.bind('<Button-1>',into) jian.bind('<Button-1>',into) q.bind('<Button-1>',into) b.bind('<Button-1>',into) j.bind('<Button-1>',into) cheng.bind('<Button-1>',into) z.bind('<Button-1>',into) sclear.bind('<Button-1>',clear) deng.bind('<Button-1>',den) dian.bind('<Button-1>',into) chu.bind('<Button-1>',into) mainloop()