鱼C论坛

 找回密码
 立即注册
查看: 518|回复: 1

[已解决]tkinter grid问题

[复制链接]
发表于 2020-4-15 19:59:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

最佳答案
2020-4-15 20:15:23
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-15 20:15:23 | 显示全部楼层    本楼为最佳答案   
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-26 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表