#๐ tkinter: root.quit() vs root.destroy()
27 messages ยท Page 1 of 1 (latest)
@sterile rain
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.
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
got it
if all i have in my window is a few labels and buttons, does it matter which one i use?
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
I've never seen destroy
and ive never seen quit
Or knew it existed
there does seem to be a tangible difference in that widgets don't get their destroy methods called when calling quit()
https://paste.pythondiscord.com/R4BQ
clicking destroy shows the destroy messages for MyTk and MyWidget before graceful exit, while clicking quit only shows graceful exit message
hmm, it seems the technical difference is that destroy invokes the Tk destroy command on the widget and its children
https://github.com/python/cpython/blob/v3.12.2/Lib/tkinter/__init__.py#L2383-L2391
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```
while Tk.quit() only tells the mainloop to stop running at the next iteration
https://github.com/python/cpython/blob/v3.12.2/Modules/_tkinter.c#L2743-L2749
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;
}```
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...
fwiw i don't see quit() mentioned in either the python docs or TkDocs
seems weird how poorly documented they are
i guess because the difference is usually unimportant?
huh
hmm, the customtkinter library seems to use quit() during testing
https://github.com/search?q=repo%3ATomSchimansky%2FCustomTkinter quit&type=code
but at the same time, they also override the destroy() method in a bunch of their widgets
https://github.com/search?q=repo%3ATomSchimansky%2FCustomTkinter+destroy&type=code
i guess for customtkinter at least, destroying the root would be safer than quitting
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.