|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题 不知道怎么改
Traceback (most recent call last):
File "Z:\python--exam\case22 Tkcase5\case22 Tkcase5.py", line 17, in <module>
validatecommand=(testCMD,'%P')).grid(row=0,column=0)
File "E:\python3.7\lib\tkinter\__init__.py", line 2223, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
代码
from tkinter import *
master = Tk()
frame = Frame(master)
frame.pack(padx=10,pady=10)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
def test(content):
return content.isdigit()
testCMD = master.register(test)
e1 = Entry(master,width=10,textvariable=v1,validate='key',\
validatecommand=(testCMD,'%P')).grid(row=0,column=0) #17行
Label(master,text='+').grid(row=0,colum=1)
e2 = Entry(master,width=10,textvariable=v2,validate='key',\
validatecommand=(testCMD,'%P')).grid(row=0,column=2)
Label(master,text='=').grid(row=0,colum=3)
e2 = Entry(master,width=10,textvariable=v3,state='readonly').grid(row=0,column=4)
def calc():
result = int(v1.get()) + int(v2.get())
v3.set(str(result))
Button(master,text='计算结果',command=calc).grid(row=3,column=2,padx=5,)
mainloop()
1. 需要先赋值再 grid()
2. column 拼错了
3. frame 也要用 grid(),不能用 pack()
代码帮你改好了:
from tkinter import *
master = Tk()
frame = Frame(master)
frame.grid()
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
def test(content):
return content.isdigit()
testCMD = master.register(test)
e1 = Entry(master, width=10, textvariable=v1, validate='key',
validatecommand=(testCMD, '%P'))
e1.grid(row=0, column=0)
Label(master, text='+').grid(row=0, column=1)
e2 = Entry(master, width=10, textvariable=v2, validate='key',
validatecommand=(testCMD, '%P'))
e2.grid(row=0, column=2)
Label(master, text='=').grid(row=0, column=3)
e3 = Entry(master, width=10, textvariable=v3, state='readonly')
e3.grid(row=0, column=4)
def calc():
result = int(v1.get()) + int(v2.get())
v3.set(str(result))
Button(master, text='计算结果', command=calc).grid(row=3, column=2, padx=5, )
mainloop()
|
|