tkinter
from tkinter import *def sumfun():
result=int(e1.get())+int(e2.get())
v3.set(result)
def test(content):
return content.isdigit()
root=Tk()
v1=StringVar()
v2=StringVar()
v3=StringVar()
testcmd=root.register(test)
e1=Entry(root,textvariable=v1,validate='key',validatecommand=(testcmd,'%p'))
e2=Entry(root,textvariable=v2,validate='key',validatecommand=(testcmd,'%p'))
e3=Entry(root,textvariable=v3,state='readonly')
e1.grid(row=0,column=0,padx=10,pady=5)
e2.grid(row=0,column=2,padx=10,pady=5)
e3.grid(row=0,column=4,padx=10,pady=5)
Label(root,text='+').grid(row=0,column=1)
Label(root,text='=').grid(row=0,column=3)
Button(root,text='计算结果',command=sumfun).grid(row=1,column=2)
mainloop()
为啥输入不了数字呢
是 %P 大写的,不是 %p
参考代码:
from tkinter import *
def sumfun():
result=int(e1.get())+int(e2.get())
v3.set(result)
def test(content):
return content.isdigit()
root=Tk()
v1=StringVar()
v2=StringVar()
v3=StringVar()
testcmd=root.register(test)
e1=Entry(root,textvariable=v1,validate='key',validatecommand=(testcmd,'%P'))
e2=Entry(root,textvariable=v2,validate='key',validatecommand=(testcmd,'%P'))
e3=Entry(root,textvariable=v3,state='readonly')
e1.grid(row=0,column=0,padx=10,pady=5)
e2.grid(row=0,column=2,padx=10,pady=5)
e3.grid(row=0,column=4,padx=10,pady=5)
Label(root,text='+').grid(row=0,column=1)
Label(root,text='=').grid(row=0,column=3)
Button(root,text='计算结果',command=sumfun).grid(row=1,column=2)
mainloop()
页:
[1]