|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from tkinter import *
list1 = ['+','-','*','/']
root = Tk()
frame = Frame(root,padx = 10, pady = 10)
frame.pack(padx = 10,pady = 10)
v1 = IntVar()
v2 = IntVar()
v3 = IntVar()
v_fuhao = StringVar()
def check_is_num(content):
if content.isdigit():
return True
else:
return False
#开始搞加减乘除和计算的函数:
def add():
v_fuhao.set(list1[0])
def jian():
v_fuhao.set(list1[1])
def cheng():
v_fuhao.set(list1[2])
def chu():
v_fuhao.set(list1[3])
def calc():
if v_fuhao.get() == "+":
v3.set(int(v1.get()) + int(v2.get()))
elif v_fuhao.get() == "-":
v3.set(int(v1.get()) - int(v2.get()))
elif v_fuhao.get() == "*":
v3.set(int(v1.get()) * int(v2.get()))
elif v_fuhao.get() == "/":
v3.set(int(v1.get()) / int(v2.get()))
# 开始封装函数:
check_is_num = root.register(check_is_num)
#开始设置entry 与 label
entry1 = Entry(frame,textvariable = v1,validate = 'key',validatecommand = (check_is_num,"%P"),\
width = 10)
entry1.grid()
label1 = Label(frame,textvariable = v_fuhao,width = 5)
label1.grid(row = 0, column = 1)
entry2 = Entry(frame,textvariable = v2,validate = 'key',validatecommand = (check_is_num,"%P"),\
width = 10)
entry2.grid(row = 0, column = 2,padx = 50)
label2 = Label(frame,text = "=")
label2.grid(row = 0, column = 3)
entry3 = Entry(frame,textvariable = v3,validate = 'key',validatecommand = (check_is_num,"%P"),\
width = 10,state = 'readonly')
entry3.grid(row = 0,column = 4)
#开始设置button:
button1 =Button(frame,text = list1[0],command = add,width = 10)
button2 =Button(frame,text = list1[1],command = jian,width = 10)
button3 =Button(frame,text = list1[2],command = cheng,width = 10)
button4 =Button(frame,text = list1[3],command = chu,width= 10)
button_calc = Button(frame,text = "计算结果",command = calc, width = 10)
button1.grid(row = 1, column = 0)
button2.grid(row = 1, column = 1)
button3.grid(row = 2, column = 0)
button4.grid(row = 2, column = 1)
button_calc.grid(row = 2, column = 4,sticky = 'w',pady = 10)
mainloop()
为什么显示框内出现的零无法被删除(用Backsapce没用/(ㄒoㄒ)/~~)
验证问题,自己慢慢研究。
def check_is_num(content):
if content=='' or content.isdigit():
return True
else:
return False
当你删到最前一位时,就为空了,为空不会通过验证。
|
|