#๐Ÿ”’ tkinter: root.quit() vs root.destroy()

27 messages ยท Page 1 of 1 (latest)

sterile rain
#

After browsing a few stackoverflow threads, I am still confused. What is the difference between quit and destroy?

onyx nexusBOT
#

@sterile rain

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

nova dagger
#

root.quit() terminates the main loop. It allowes the code to stop. It does not immedently termainate, and lets it exit gracefully.

root.destroy() immediately destroys the window with widgets and other things. typically used when you want to close the application abruptly

sterile rain
#

got it

#

if all i have in my window is a few labels and buttons, does it matter which one i use?

nova dagger
#

I'd just use the .quit()

#

quit is like the peaceful closing of it

quartz egret
#

huh, most of the scripts ive seen use destroy even during graceful exits

#

that method gets called on your widgets when the user clicks the close button too

nova dagger
#

I've never seen destroy

quartz egret
#

and ive never seen quit

nova dagger
#

Or knew it existed

quartz egret
#

clicking destroy shows the destroy messages for MyTk and MyWidget before graceful exit, while clicking quit only shows graceful exit message

onyx nexusBOT
#

Lib/tkinter/__init__.py lines 2383 to 2391

def destroy(self):
    """Destroy this and all descendants widgets. This will
    end the application of this Tcl interpreter."""
    for c in list(self.children.values()): c.destroy()
    self.tk.call('destroy', self._w)
    Misc.destroy(self)
    global _default_root
    if _support_default_root and _default_root is self:
        _default_root = None```
quartz egret
onyx nexusBOT
#

Modules/_tkinter.c lines 2743 to 2749

static PyObject *
_tkinter_tkapp_quit_impl(TkappObject *self)
/*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/
{
    quitMainLoop = 1;
    Py_RETURN_NONE;
}```
quartz egret
#

i dont notice any cleanup of the Tcl interpreter in _tkinter_tkapp_mainloop_impl, though its not obvious to me what cleanup Tk does when the root window is destroyed either...

#

seems weird how poorly documented they are

#

i guess because the difference is usually unimportant?

sterile rain
#

huh

quartz egret
#

i guess for customtkinter at least, destroying the root would be safer than quitting

onyx nexusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.