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

时间:2018-08-17 04:23:56

标签: python-3.x inheritance tkinter destroy

我在杀死使用以下方式创建的tkinter窗口时遇到了很多麻烦。我收到下面显示的错误。我是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"

2 个答案:

答案 0 :(得分:0)

您必须保留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()

答案 1 :(得分:0)

问题是这行代码:

www.site.com/folder/conn.php

与您执行此操作完全相同:

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

由于result = self.send_email() self.yes['command'] = reuslt 破坏了根窗口,然后返回self.send_email(),因此与之相同:

None

一旦销毁了根窗口小部件(这会导致所有其他窗口小部件都被破坏),则每次尝试在窗口小部件上调用方法时都会收到错误消息。当您尝试配置root.destroy() self.yes['command'] = None 时,self.yes不再存在,因此会出现错误。

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

self.yes

请注意,self.yes['command'] = self.send_email 上没有括号。而不是立即调用该函数,而是告诉tk“这是我希望您在用户单击按钮时调用的函数的名称”。