gpt4 book ai didi

python - Tkinter 错误 : cannot use geometry manager grid inside . 已经有由包管理的从站

转载 作者:行者123 更新时间:2023-12-01 09:13:23 24 4
gpt4 key购买 nike

from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk,width=1000,height=1000)
canvas.pack()
i=0
x1=-50
y1=0
choice=None
canvas.create_text(500, 300, text="Choose one.", font='TNR 20 bold')
def car():
choice='car'
return choice


button1=Button(tk,text='car',width=10,command=car)
button1.grid(row=1,column=5)#I do not see anything wrong here
tk.mainloop()

如果您想知道为什么我有这么多未使用的变量,这只是我的代码的一部分。

这是我运行代码时所说的内容:

  Traceback (most recent call last):  File "(You dont need to know the file name)", line 180, in     button1.grid(row=1,column=5)  File "C:\Users\()\AppData\Local\Programs\Python\Python37-32\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

最佳答案

不能混合使用 pack()grid()
来自文档:

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

您正在调用 canvas.pack(),但对于同一个 tk 对象,您添加一个 button 并在其上调用 grid() 函数。

关于python - Tkinter 错误 : cannot use geometry manager grid inside . 已经有由包管理的从站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com