1062195630 发表于 2021-6-11 15:45:00

python tkinter模块

from tkinter import *
def sumfun():
    aa=int(v1.get())+int(v2.get())
    v3.set(aa)
def isd(content):
    if content.isdigit():
      return 1
    else:
      return 0
root=Tk()
frame=Frame(root)
frame.pack(padx=10,pady=5)
isdcmd=root.register(isd)
v1=StringVar()
v2=StringVar()
v3=StringVar()
e1=Entry(frame,textvariable=v1,validate='key',validatecommand=(isdcmd,'P'))
e1.grid(row=0,column=0)
e2=Entry(frame,textvariable=v2,validate='key',validatecommand=(isdcmd,'P'))
e2.grid(row=0,column=2)
e3=Entry(frame,textvariable=v3)
e3.grid(row=0,column=4)
Label(frame,text="+").grid(row=0,column=1)
Label(frame,text="=").grid(row=0,column=3)
Button(frame,text='计算结果',command=sumfun).grid(row=1,column=2)
mainloop()



我的为什么输入不进去数字啊

Twilight6 发表于 2021-6-11 15:57:43


验证字符串你忘记加 % 号了

validatecommand=(isdcmd,'%P')

参考代码:

from tkinter import *


def sumfun():
    aa = int(v1.get()) + int(v2.get())
    v3.set(aa)


def isd(content):
    if content.isdigit():
      return 1
    else:
      return 0


root = Tk()
frame = Frame(root)
frame.pack(padx=10, pady=5)
isdcmd = root.register(isd)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
e1 = Entry(frame, textvariable=v1, validate='key', validatecommand=(isdcmd, '%P'))
e1.grid(row=0, column=0)
e2 = Entry(frame, textvariable=v2, validate='key', validatecommand=(isdcmd, '%P'))
e2.grid(row=0, column=2)
e3 = Entry(frame, textvariable=v3)
e3.grid(row=0, column=4)
Label(frame, text="+").grid(row=0, column=1)
Label(frame, text="=").grid(row=0, column=3)
Button(frame, text='计算结果', command=sumfun).grid(row=1, column=2)
mainloop()

叼辣条闯世界 发表于 2021-6-12 12:25:52

from tkinter import *


def sumfun():
    aa = int(v1.get()) + int(v2.get())
    v3.set(aa)


def isd(content):
    if content.isdigit():
      return 1
    else:
      return 0


root = Tk()
frame = Frame(root)
frame.pack(padx=10, pady=5)
isdcmd = root.register(isd)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
e1 = Entry(frame, textvariable=v1, validate='key', validatecommand=(isdcmd, '%P'))
e1.grid(row=0, column=0)
e2 = Entry(frame, textvariable=v2, validate='key', validatecommand=(isdcmd, '%P'))
e2.grid(row=0, column=2)
e3 = Entry(frame, textvariable=v3)
e3.grid(row=0, column=4)
Label(frame, text="+").grid(row=0, column=1)
Label(frame, text="=").grid(row=0, column=3)
Button(frame, text='计算结果', command=sumfun).grid(row=1, column=2)
mainloop()
页: [1]
查看完整版本: python tkinter模块