【问题标题】:tkinter wait_window() raising tkinter.TclError: Bad window path nametkinter wait_window() 引发 tkinter.TclError: 错误的窗口路径名
【发布时间】:2016-01-02 12:32:10
【问题描述】:

一直在玩python的tkinter,写了如下代码进行对话练习:

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()

所以Dialog是App打开的对话框,用于修改label的文字等。 虽然它似乎运行没有太大问题,但控制台显示以下错误:

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?)而引发的,但我不明白为什么在这种情况下会引发这个错误。

请帮忙!

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    这是因为对话框本身在调用wait_window,然后在它被销毁后主程序也在调用wait_window。第二次调用要等到第一次退出后才能发生,而第一次调用要等到窗口被销毁后才能退出。

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 2020-02-03
      • 2021-01-12
      • 2020-06-10
      • 2020-11-14
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多