from tkinter import * root=Tk() root.title("计算器") result=StringVar() result.set("") label=Label(root,width=25,height=2,relief="raised",\ anchor=SE,textvariable=result) label.grid(row=0,column=0,columnspan=4,padx=5,pady=5) Button(root,text="C",width=5,command=lambda:result.set("") ).grid(row=1,column=0) Button(root,text="DEL",width=5).grid(row=1,column=1) Button(root,text="BCK",width=5,command=lambda:result.set(result.get()[:-1])).grid(row=1,column=2) Button(root,text="7",width=5,command=lambda:result.set(result.get()+"7") ).grid(row=2,column=0) Button(root,text="8",width=5,command=lambda:result.set(result.get()+"8") ).grid(row=2,column=1) Button(root,text="9",width=5,command=lambda:result.set(result.get()+"9") ).grid(row=2,column=2) Button(root,text="/",width=5,command=lambda:result.set(result.get()+"9") ).grid(row=1,column=3) Button(root,text="4",width=5,command=lambda:result.set(result.get()+"4") ).grid(row=3,column=0) Button(root,text="5",width=5,command=lambda:result.set(result.get()+"5") ).grid(row=3,column=1) Button(root,text="6",width=5,command=lambda:result.set(result.get()+"6") ).grid(row=3,column=2) Button(root,text="*",width=5,command=lambda:result.set(result.get()+"*") ).grid(row=2,column=3) Button(root,text="1",width=5,command=lambda:result.set(result.get()+"1") ).grid(row=4,column=0) Button(root,text="2",width=5,command=lambda:result.set(result.get()+"2") ).grid(row=4,column=1) Button(root,text="3",width=5,command=lambda:result.set(result.get()+"3") ).grid(row=4,column=2) Button(root,text="-",width=5,command=lambda:result.set(result.get()+"-") ).grid(row=3,column=3) Button(root,text="0",width=5,command=lambda:result.set(result.get()+"0") ).grid(row=5,column=0) Button(root,text=".",width=5,command=lambda:result.set(result.get()+".") ).grid(row=5,column=1) Button(root,text="=",width=12,command=lambda:result.set(result.get()+"="+str(eval(result.get()))) ).grid(row=5,column=2,columnspan=2) Button(root,text="+",width=5,command=lambda:result.set(result.get()+"+") ).grid(row=4,column=3) root.mainloop()
简易计算器程序设计
最新推荐文章于 2024-08-15 18:19:19 发布