_tkinter.TclError:无法使用几何管理器网格。已经有packenter代码管理的奴隶

时间:2015-05-08 08:18:31

标签: python tkinter

我尝试根据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继承我有什么不妥。

1 个答案:

答案 0 :(得分:2)

您的super()电话看起来不对。您需要传递父窗口小部件并传递解压缩的关键字参数,如下所示:

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