python用户界面设计计算器_Python tkinter 界面设计——小小计算器

#本程序通过简单的四则混合运算,学习界面设计、按键获取

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值