python Tkinter,检查根是否已被破坏? [英] python Tkinter, check if root has been destroyed?

查看:137
本文介绍了python Tkinter,检查根是否已被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter和线程编写应用程序.

I am writing an application using Tkinter along with threading.

我遇到的问题是,在关闭主应用程序后,某些线程仍在运行,并且我需要一种方法来检查根窗口是否已被破坏以避免TclError: can't invoke "wm" command.

The problem I got is, after closing the main app, some thread is still running, and I need a way to check whether the root windows has been destroyed to avoid the TclError: can't invoke "wm" command.

我知道的所有方法:wminfo_exists()state()一旦根被破坏,都将返回错误.

All methods I know: wminfo_exists() and state() all return error once the root is destroyed.

推荐答案

如果有人遇到相同问题,我将为此添加解决方法.我遵循了此处的建议.我拦截了Windows的关闭事件,以设置标记root已死的标志,并在需要时检查该标志.

I will add my workaround for this, in case anyone came across the same issue. I was following the suggestion from here. I intercept the windows' closing event to set my flag that marks the root is already dead, and check for that flag when I need.

exitFlag = False

def thread_method():
    global root, exitFlag
    if not exitFlag:
        // execute the code relate to root

def on_quit():
    global exitFlag
    exitFlag = True
    root.destroy()

root.protocol("WM_DELETE_WINDOW", on_quit)

这篇关于python Tkinter,检查根是否已被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆