gpt4 book ai didi

python - _tkinter.TclError : cannot use geometry manager grid inside . 这里已经有由 packenter 代码管理的从站

转载 作者:行者123 更新时间:2023-12-01 04:43:04 26 4
gpt4 key购买 nike

我正在尝试创建一个基于 Entry 小部件的子小部件。不幸的是它没有像我想象的那样工作。来自以下代码:

import tkinter as tk

WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600


class Application(tk.Frame):

def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "SOLVE"
self.hi_there.grid(row=1, column=1)

self.QUIT = tk.Button(
self, text="QUIT", fg="red", command=self.master.quit)
self.QUIT.grid(row=1, column=2)

# self.number_box = tk.Entry(self) # This is working
self.number_box = NumberBox(self) # this is not working
self.number_box.grid(row=2, column=1)


class MainWindow():

def __init__(self):
#self.root = tk.Tk()
self.app = Application()
self.app.master.title("SUDOKU")
self.app.master.minsize(width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
self.app.master.maxsize(width=WINDOW_WIDTH, height=WINDOW_HEIGHT)
self.app.mainloop()


class NumberBox(tk.Entry):

def __init__(self, master=None, cnf={}, **kw):

super().__init__(cnf, kw)
#self.text = tk.StringVar()


window = MainWindow()

我收到错误:

Traceback (most recent call last):
File "E:/workspace/python/sudoku/gui/guitk.py", line 42, in <module>
window = MainWindow()
File "E:/workspace/python/sudoku/gui/guitk.py", line 28, in __init__
self.app = Application()
File "E:/workspace/python/sudoku/gui/guitk.py", line 11, in __init__
self.createWidgets()
File "E:/workspace/python/sudoku/gui/guitk.py", line 22, in createWidgets
self.number_box.grid(row=2, column=1)
File "C:\Python34\lib\tkinter\__init__.py", line 2057, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here

当我直接使用 Entry 类(而不是 NumberBox,请参阅注释行)时,代码可以正常工作。我从 Entry 继承有什么问题导致我的类无法正常工作。

最佳答案

您的 super() 调用看起来错误。您需要传递父窗口小部件并传递解压的关键字参数,如下所示:

super().__init__(master, cnf, **kw)

关于python - _tkinter.TclError : cannot use geometry manager grid inside . 这里已经有由 packenter 代码管理的从站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119056/

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