2016-01-02 40 views
0

我一直在使用Python的Tkinter的瞎搞,並寫了對話練習下面的代碼:Tkinter的wait_window()提高tkinter.TclError:壞窗口路徑名

import tkinter as tk 

class Dialog(tk.Toplevel): 
    def __init__(self, parent, title=None): 
     tk.Toplevel.__init__(self, parent) 
     self.transient(parent) 
     if title: self.title(title) 
     self.parent = parent 
     self.result = None 
     body = tk.Frame(self) 
     self.e = tk.Entry(body) 
     self.e.pack() 
     self.initial_focus = self.e 
     body.pack(padx=5, pady=5) 
     self.buttonbox() 
     self.grab_set() 
     if not self.initial_focus: self.initial_focus = self 
     self.protocol("WM_DELETE_WINDOW", self.cancel) 
     self.geometry("+%d+%d"%(parent.winfo_rootx()+50, parent.winfo_rooty()+50)) 
     self.initial_focus.focus_set() 
     self.wait_window(self) 
    def buttonbox(self): 
     box = tk.Frame(self) 
     w = tk.Button(box, text="OK", width=10, command=self.ok, default=tk.ACTIVE) 
     w.pack(side=tk.LEFT, padx=5, pady=5) 
     w = tk.Button(box, text="Cancel", width=10, command=self.cancel) 
     w.pack(side=tk.LEFT, padx=5, pady=5) 
     self.bind("<Return>", self.ok) 
     self.bind("<Escape>", self.cancel) 
     box.pack() 
    def ok(self, event=None): 
     if not self.validate(): 
      self.initial_focus.focus_set() 
      return 
     self.withdraw() 
     self.update_idletasks() 
     self.apply() 
     self.cancel() 
    def cancel(self, event=None): 
     self.parent.focus_set() 
     self.destroy() 
    def validate(self): 
     if self.e.get(): return True 
     else: return False 
    def apply(self): 
     value = self.e.get() 
     self.parent.l.config(text=value) 

class App(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 
     self.l = tk.Label(self, text="Hello!") 
     self.b = tk.Button(self, text="Hello?", command=self.dialog, default=tk.ACTIVE) 
     self.l.pack() 
     self.b.pack() 
    def dialog(self): 
     self.dialogbox = Dialog(self) 
     self.dialogbox.wait_window() 

app = App() 
app.mainloop() 

所以對話框是打開一個對話框應用程序,它是用來改變標籤的文本等 雖然它似乎沒有太大的問題跑,下面的錯誤控制檯顯示:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__ 
    return self.func(*args) 
    File "C:\Users\Administrator\Documents\JW\Python 3.5\guiPractice.py", line 58, in dialog 
    self.dialogbox.wait_window() 
    File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 490, in wait_window 
    self.tk.call('tkwait', 'window', window._w) 
_tkinter.TclError: bad window path name ".9605808" 

從我發現,它的提出是因爲我想處理一個被破壞的對象(self.dialogbox?),但我看不出爲什麼exac在這種情況下提出這個錯誤。

請幫忙!

回答

1

這是因爲對話本身本身就是調用wait_window,然後在它被銷燬之後主程序也叫wait_window。第二次調用只有在第一次退出後才能發生,第一次調用不能退出,直到窗口被銷燬。