Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter: Check if root has been destroyed?

I am writing an application using Tkinter along with threading.

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.

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

like image 435
bizi Avatar asked Nov 18 '13 02:11

bizi


Video Answer


1 Answers

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)
like image 175
bizi Avatar answered Oct 28 '22 14:10

bizi