无法销毁继承的 tkinter.Tk() window

Can't destroy inherited tkinter.Tk() window

我在杀死使用以下方式创建的 tkinter window 时遇到了很多麻烦。我收到如下所示的错误。我是 Python 的新手,如有任何帮助,我们将不胜感激。

class InspectWindow(tk.Frame):
    def __init__(self, sender_email, recipient_email, email_body, 
                   master = None):
        super().__init__(master)
        self.create_widgets()

def create_widgets(self):
    self.yes = tk.Button(self)
    self.yes['text'] = 'send me!'
    self.yes['command'] = self.send_email()

def send_email(self):
    root.destroy()


root = tk.Tk()
popup = InspectWindow(sender_email, recipient_email, email_body,
                        master=root)
popup.mainloop()



Traceback (most recent call last):
  File "spam.py", line 108, in <module>
    master=root)
  File "spam.py", line 16, in __init__
    self.create_widgets()
  File "AutomateFellowshipEmails.py", line 23, in create_widgets
    self.yes['command'] = self.send_email()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1486, in __setitem__
    self.configure({key: value})
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1479, in configure
    return self._configure('configure', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1470, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!inspectwindow.!button"

你必须保留 master 的引用才能调用它的方法。
(还有一些其他错误)

像这样(我删除了未定义的变量 mail,等等...)

import tkinter as tk

class InspectWindow(tk.Frame):          # if this is a popup, you may like to inherit from tk.TopLevel?

    def __init__(self, master = None):  # removed undefined variables, you can put them back in
        self.master = master            # keep a reference of master
        super().__init__(self.master)
        self.create_widgets()
        self.pack()                     # pack into parent window

    def create_widgets(self):
        self.yes = tk.Button(self)
        self.yes['text'] = 'send me!'
        self.yes['command'] = self.send_email   # removed () call, this takes a method, not a method call
        self.yes.pack()                 # pack into self

    def send_email(self):
        self.master.destroy()           # call destroy on self.master
                                        # maybe you want to call on self instead, depending what you want to destroy


root = tk.Tk()
popup = InspectWindow(master=root)      # removed undefined variables, you can put them back in
popup.mainloop()

问题是这行代码:

self.yes['command'] = self.send_email()

这与您这样做完全相同:

result = self.send_email()
self.yes['command'] = reuslt

因为self.send_email()破坏根window然后returnsNone,就等于这样做:

root.destroy()
self.yes['command'] = None

一旦您销毁根小部件(这会导致所有其他小部件被销毁),您将在任何时候尝试调用小部件上的方法时遇到错误。当您尝试配置 self.yes 时,self.yes 不再存在,因此出现错误。

解决方案是将 reference 传递给按钮的功能,这样您就不会立即调用它。你这样做:

self.yes['command'] = self.send_email

请注意 self.send_email 上缺少括号。您不是立即调用函数,而是告诉 tk "here is the name of a function that I want you to call when the user clicks the button".