我在我的tkinter应用程序中也有类似的问题。我从写的帖子中学到了我可以为这样的窗口删除设置回调:
def closing_cbk():
# Shutdown procedure
root.quit()
root.destroy()
...
root.protocol("WM_DELETE_WINDOW", closing_cbk)
closing_cbk()
sys.exit()
的内容。
state()
中使用Tk
检查窗口是否打开。窗口打开时,它应该返回
'normal'
,如果窗口未打开,它将返回其他内容。我要做的就是添加一些检查窗口状态并在关闭应用时退出的东西。
while app_is_running:
if root.state() != 'normal':
sys.exit(0)
使用此构造在我的公司笔记本电脑上,在Windows 11上看到了这个问题:
出于某种原因,我无法在运行同一python版本的个人计算机上复制它,而Windows11
但是,如果您看到它并搜索了它,就像我在这个古老的讨论中结束了,我会告诉您,它使用了@Wilson发布的答案,并且对我有用。如果您使用类似的构造,请通过包装标准退出方法来实现它,然后在此包装事件处理程序中添加销毁功能。
class GUI(tk.Tk):
#existing code remains here
#
def exit(self):
self.quit()
self.destroy()
print("wBaseTitle Exited, have a nice day")
#
#
def main(argv):
app = GUI()
app.mainloop()
app.protocol("WM_DELETE_WINDOW", app.exit)