TclError:错误的窗口路径名称(Python)

时间:2017-11-20 04:49:43

标签: python python-2.7 tkinter tkinter-layout

在其中一个视图中有一个用于关闭实际视图的按钮,它可以工作,但当我再次尝试打开视图时,它会向我显示下一个错误:

Exception in Tkinter callback
Traceback (most recent call last):
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1545, in __call__
        return self.func(*args)
      File "/home/htm97/Documents/data/workspace/repositories/projects-h/locker-system/src/gui/MainMenu.py", line 27, in verify_lockers_window
        self.app = vl.Lockers(self.vlWindow)
      File "/home/htm97/Documents/data/workspace/repositories/projects-h/locker-system/src/gui/Lockers.py", line 19, in __init__
        self.buttonsList[i].grid(columnspan = 4)
      File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2032, in grid_configure
        + self._options(cnf, kw))
TclError: bad window path name ".140687059771120.140687059776216.140687059776504.140687059776576"

销毁窗口的功能是:

def close_windows(self):                                                                                                                   
    "This function destroys the window"                                                                                                    
    self.master.destroy() 

这是观点:

import Tkinter as tk
class Lockers:
    lockerList = ["1", "2", "3", "4", "5"]
    buttonsList = []

    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.master.minsize(width = 250, height = 200)
        self.initialize_lockers()

        self.frame.grid()

        tk.Label(self.frame, text = "Seleccione el locker que desea revisar:").grid(row = 0, columnspan = 4)

        i = 0
        while i < len(self.lockerList):
            self.buttonsList[i].grid(columnspan = 4) #HERE THE ERROR
            i += 1

        tk.Label(self.frame, text = "").grid(columnspan = 4)
        self.quitButton = tk.Button(self.frame, text = 'Salir', width = 8, command = self.close_windows)
        self.quitButton.grid(column = 1, columnspan = 2)

函数initialize_lockers()将一些按钮附加到buttonsList。

阅读一段时间后,我发现在执行destroy()并尝试网格化后会显示错误,但我不明白为什么。

1 个答案:

答案 0 :(得分:1)

问题是我在方法之外声明了buttonsList作为属性,当我销毁类的实例时,属性消失了,所以我不得不在构造函数中声明列表,这样做我有没有破坏的问题。