Python:使用deiconify时的错误窗口路径名称

时间:2014-12-01 06:57:51

标签: python python-3.x tkinter

我正在使用python程序遇到这个问题我正在制作如果我显示TopLevel窗口,在这种情况下我的帮助菜单,然后撤回然后尝试再次显示它我得到以下错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\****\Documents\GitHub\ProjectName\ProjectName\GUI.py", line 60, in     displayHelp
    self.helpMenu.display();
  File "C:\Users\****\Documents\GitHub\ProjectName\ProjectName\HelpMenu.py", line 35, in display
    self.deiconify();
  File "C:\Python34\lib\tkinter\__init__.py", line 1646, in wm_deiconify
    return self.tk.call('wm', 'deiconify', self._w)
_tkinter.TclError: bad window path name ".60000336"

当我从HelpMenu.py中退出并使用deiconify从GUI.py文件重新显示时,错误首先发生。 从那时起,我尝试了多种方法来解决问题,包括从HelpMenu.py中调用deiconify,并在撤消时更新存储在GUI中的帮助菜单副本。 我正在运行Python 3.4.2

我已经在线进行了大量搜索,但未能找到问题的解决方案。我发现了其他有关此错误的提及,但它们要么与我的情况无关,要么解决方案不起作用。

以下是HelpMenu.py的完整代码,后跟GUI.py的摘录,该摘录保留了重现错误但删除了其他代码的功能。

#!/usr/bin/python
try:
    from Tkinter import *
except ImportError:
    from tkinter import *

class HelpMenu(Toplevel):
    def __init__(self, parent, observer):
        Toplevel.__init__(self);
        self.observer = observer;#Observer is the GUI, this is here just so I can update the GUI when I withdraw this window
        self.setup();
        self.withdraw();
        self.protocol('WM_DELETE_WINDOW', self.quit());#Changes the close button to just hide the window

    def setup(self):
       self.columnconfigure(0,weight=1);
       w = 400;#Sets up the window position on the screen
       h = 150;
       sw = self.winfo_screenwidth();
       sh = self.winfo_screenheight();
       x=(sw-w)/2;
       y =(sh-h)/2;
       self.update();
       self.geometry('%dx%d+%d+%d' % (w,h,x,y));
       self.resizable(width=0, height=0);
       self.grid();
       self.title("Help Menu");
    def quit(self):#Hides the window
       self.withdraw();
       self.observer.updateHelp(self);
    def display(self):#Re-displays the window
        self.deiconify();

以下是从GUI.py获取的代码,并修改为只有重现问题所需的代码。

    #!/usr/bin/python
#Allows compatibility with any version of Python by checking for both versions of Tkinter
try:
    from Tkinter import *
except ImportError:
    from tkinter import *
#Imports the AutoCompleteEntry
from HelpMenu import HelpMenu

class UI(Tk):
    def initialize(self):
        #Handles setting up most of the GUI
        w = 500;#Window width
        h = 500;#Window height
        sw = self.winfo_screenwidth();#Gets screen width
        sh = self.winfo_screenheight();#Gets screen height
        x=(sw-w)/2;#Calculates the x position for the left side of the window that allows it to be placed in the center of the screen
        y =(sh-h)/2;#Calculates the y position for the top of the window that allows it to be placed in the center of the screen
        self.update();#Forces and update on the window
        self.geometry('%dx%d+%d+%d' % (w,h,x,y));#Sets the windows width, height and position
        self.minsize(int(w),int(h/2));#Sets the minimum size of the window
        self.configureMenu();
    def updateHelp(self, helpMenu):
        self.helpMenu=helpMenu;
    def displayHelp(self):
        self.helpMenu.display();
    def configureMenu(self):
        #Handles configuring and setting up the menus
        menu = Menu(self);#Setup the menu bar
        menu.add_command(label="Help",command=self.displayHelp);
        self.config(menu=menu);
    def __init__(self, parent):
     #Handles the initial call to create a GUI
        Tk.__init__(self,parent);#Parent constructor
        self.parent = parent;#Store the parent
        self.initialize();#Initilize the GUI
        self.helpMenu = HelpMenu(self, self);
        self.mainloop();#Start the main loop
if __name__ == "__main__":
    import sys
    main = UI(None);

最后一点,我对Python稍微有点新意,所以我的代码中可能还有其他错误,而我不介意如果他们被指出,我现在主要关注的是修复此路径名错误。

编辑:差不多一个月了,我还没有找到问题的解决方案。任何帮助都会很棒但是在这一点上我可能不得不放弃我的项目。

2 个答案:

答案 0 :(得分:1)

所以,休息一下后我又回去看了这个问题。

事实证明问题是self.protocol(' WM_DELETE_WINDOW',self.quit())实际上并没有调用self.quit()并且正在彻底摧毁窗口。

快速更改self.protocol(' WM_DELETE_WINDOW',self.quit)似乎修复了它。

答案 1 :(得分:0)

我想也许逗号会导致问题。试着这样写:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

class HelpMenu(Toplevel):
    def __init__(self, parent, observer):
        Toplevel.__init__(self)
        self.observer = observer  # Observer is the GUI, this is here just so I can update the GUI when I withdraw this window
        self.setup()
        self.withdraw()
        self.protocol('WM_DELETE_WINDOW', self.quit())  # Changes the close button to just hide the window

    def setup(self):
        self.columnconfigure(0, weight=1)
        w = 400  # Sets up the window position on the screen
        h = 150
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()
        x = (sw - w) / 2
        y = (sh - h) / 2
        self.update()
        self.geometry('%dx%d+%d+%d' % (w, h, x, y))
        self.resizable(width=0, height=0)
        self.grid()
        self.title("Help Menu")
    def quit(self):  # Hides the window
        self.withdraw()
        self.observer.updateHelp(self)
    def display(self):  # Re-displays the window
        self.deiconify()


class UI(Tk):
    def initialize(self):
        # Handles setting up most of the GUI
        w = 500  # Window width
        h = 500  # Window height
        sw = self.winfo_screenwidth()  # Gets screen width
        sh = self.winfo_screenheight()  # Gets screen height
        x = (sw - w) / 2  # Calculates the x position for the left side of the window that allows it to be placed in the center of the screen
        y = (sh - h) / 2  # Calculates the y position for the top of the window that allows it to be placed in the center of the screen
        self.update()  # Forces and update on the window
        self.geometry('%dx%d+%d+%d' % (w, h, x, y))  # Sets the windows width, height and position
        self.minsize(int(w), int(h / 2))  # Sets the minimum size of the window
        self.configureMenu()
    def updateHelp(self, helpMenu):
        self.helpMenu = helpMenu
    def displayHelp(self):
        self.helpMenu.display()
    def configureMenu(self):
        # Handles configuring and setting up the menus
        menu = Menu(self)  # Setup the menu bar
        menu.add_command(label="Help", command=self.displayHelp)
        self.config(menu=menu)
    def __init__(self, parent):
        # Handles the initial call to create a GUI
        Tk.__init__(self, parent)  # Parent constructor
        self.parent = parent  # Store the parent
        self.initialize()  # Initilize the GUI
        self.helpMenu = HelpMenu(self, self)
        self.mainloop()  # Start the main loop
if __name__ == "__main__":
    main = UI(None)        

从myside完美运作。