#本程序通过简单的四则混合运算,学习界面设计、按键获取
from tkinter import *
top = Tk()
top.title('小小计算器')
rebegin=False
#对按键的反应处理
def presskey(keyname):
global rebegin
oldlist = formula.get()
if rebegin:
oldlist=""
rebegin=False
elif keyname == "←":
keyname=""
oldlist=oldlist[0:-1]
elif keyname == "=":
rebegin = True
try:
comlist=oldlist.replace("×","*")
comlist=comlist.replace("÷","/")
resultvalue = eval(comlist)
result.set(resultvalue)
except:
keyname = " 错误 "
newlist=oldlist+keyname
formula.set(newlist)
frame_formula= Frame(top).grid(row=0,column=0,columnspan=20)
frame_key= Frame(top).grid(row=2,column=0,columnspan=20)
formula = StringVar()
formula.set("")
f_label=Label(frame_formula,textvariable=formula, justify=LEFT).grid(row=0,column=0,columnspan=20)
result = StringVar()
result.set("小小计算器")
r_label=Label(frame_formula,textvariable=result, justify=RIGHT).grid(row=1,column=0,columnspan=20)
num_1 = Button(frame_key, text="1", command=lambda : presskey("1"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=0)
num_2 = Button(frame_key, text="2", command=lambda : presskey("2"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=1)
num_3 = Button(frame_key, text="3", command=lambda : presskey("3"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=2)
ope_a = Button(frame_key, text="+", command=lambda : presskey("+"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=4)
ope_s = Button(frame_key, text="-", command=lambda : presskey("-"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=5)
ope_m = Button(frame_key, text="×", command=lambda : presskey("×"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=2,column=6)
num_4 = Button(frame_key, text="4", command=lambda : presskey("4"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=0)
num_5 = Button(frame_key, text="5", command=lambda : presskey("5"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=1)
num_6 = Button(frame_key, text="6", command=lambda : presskey("6"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=2)
ope_d = Button(frame_key, text="÷", command=lambda : presskey("÷"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=4)
ope_l = Button(frame_key, text="(", command=lambda : presskey("("),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=5)
ope_r = Button(frame_key, text=")", command=lambda : presskey(")"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=3,column=6)
num_7 = Button(frame_key, text="7", command=lambda : presskey("7"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=0)
num_8 = Button(frame_key, text="8", command=lambda : presskey("8"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=1)
num_9 = Button(frame_key, text="9", command=lambda : presskey("9"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=2)
num_0 = Button(frame_key, text="0", command=lambda : presskey("0"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=4)
ope_p = Button(frame_key, text=".", command=lambda : presskey("."),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=5)
ope_b = Button(frame_key, text="←", command=lambda : presskey("←"),
activeforeground='white', activebackground='red', height=1,width=4).grid(row=4,column=6)
quit = Button(top, text="退出", command=top.quit,
activeforeground='white', activebackground='red', height=1,width=15).grid(row=5,column=0,columnspan=4)
equal = Button(top, text="=", command=lambda : presskey("="),
activeforeground='white', activebackground='red', height=1,width=15).grid(row=5,column=3,columnspan=4)
#运行主程序
mainloop()