【发布时间】:2019-02-02 08:02:55
【问题描述】:
我最近开始接触 python 并编写一些简单的程序,并且通过堆栈溢出和 YouTube 教程,它非常顺利。但是,当我尝试制作 Tkinter“WM_DELETE_WINDOW”协议并运行程序时。文本框将正常打开,并会随着退出文本框正确关闭,但随后会打开第二个空文本框和第二个退出文本框,并显示相同的消息。然后在我关闭后,该程序将第三次尝试销毁该盒子并出现此错误。
C:\Users\thech\Desktop\Python stuff>python spam.py
Traceback (most recent call last):
File "spam.py", line 34, in <module>
spam()
File "spam.py", line 31, in spam
if closed():
File "spam.py", line 13, in closed
mibox.destroy()
File "C:\Users\thech\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2062, in destroy
self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
如果您能查看我的代码,我将不胜感激,这里是:
from tkinter import *
from tkinter import messagebox
import time
#pop up
def spam():
global spamreturn
spamreturn = False
def closed():
if messagebox.askokcancel("Quit", "Do you really wish to quit?"):
mibox.destroy()
return True
mibox = Tk()
topframe = Frame(mibox)
miLabel = Label(mibox, text="Call 1-800-273-8255")
mibutton = Button(topframe, text="Your Computer has been infected")
mibutton2 = Button(topframe, text="Please call 1-800-273-8255 for Assistance")
miLabel.pack()
mibutton.pack()
mibutton2.pack()
topframe.pack()
mibox.geometry("300x100+500+250")
mibox.protocol("WM_DELETE_WINDOW", closed)
mibox.mainloop()
if closed():
spamreturn = True
spam()
if spamreturn == True:
print("worked")
time.sleep(3)
【问题讨论】:
-
只需删除/注释掉
if closed():和spamreturn = True并按需要工作,它会导致 closed() 函数再次执行
标签: python python-3.x tkinter