python Tkinter, check if root has been destroyed?

前端 未结 2 2002
情歌与酒
情歌与酒 2020-12-20 16:04

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 c

相关标签:
2条回答
  • 2020-12-20 16:29

    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)
    
    0 讨论(0)
  • 2020-12-20 16:33

    If you are using 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 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.

    0 讨论(0)
提交回复
热议问题