python Tkinter, check if root has been destroyed? python Tkinter, check if root has been destroyed? tkinter tkinter

python Tkinter, check if root has been destroyed?


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 = Falsedef thread_method():    global root, exitFlag    if not exitFlag:        // execute the code relate to rootdef on_quit():    global exitFlag    exitFlag = True    root.destroy()root.protocol("WM_DELETE_WINDOW", on_quit)


If you are using something like this:

import Tkinterroot = Tkinter.Tk()root.bind('<space>', lambda e: root.quit())  # quitting by pressing spacebarroot.mainloop()

and not: root.destroy() then the quit method will kill the Tcl interpreter not just breaks out from the mainloop and deletes all widgets. So once you called root.quit() you can be sure, that your root is completely dead!

All the other methods you suggested (like: wminfo_exists()) are only available when at least one valid Tk exists.


NOTE:

If you are using more than one mainloop, you should use the destroy method to make sure, that your main mainloop won't be killed -- but I don't think this is your case.