Python Tkinter, check if root has been destroyed?

I am writing an application using Tkinter along with a stream.

The problem is that after the main application closes, some thread is still working, and I need a way to check if the root windows have been damaged to avoid TclError: can't invoke "wm" command .

All the methods that I know: wminfo_exists() and state() all return an error after destroying the root.

+6
source share
2 answers

I will add a workaround for this if someone comes across the same question. I followed the suggestion here . I intercept the window close event to set a flag that notes that root already dead, and check this flag when I need to.

 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) 
+5
source

If you use something like this:

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

and not: root.destroy() , then the quit method will kill the Tcl interpreter, which will not just exit mainloop and remove all widgets. So, once you call root.quit() , you can be sure that your root completely dead!

All other methods you have proposed (for example: wminfo_exists() ) are available only when at least one valid Tk exists.


Note:

If you use more than one mainloop, you must use the destroy method to ensure that your main mainlop will not be killed - but I don’t think this is your case.

+1
source

Source: https://habr.com/ru/post/958281/


All Articles