In Python using tkinter
, what is the difference between root.destroy()
and root.quit()
when closing the root window?
Is one prefered over the other? Does one release resources that the other doesn't?
Build A Paint Program With TKinter and Python Tkinter destroy() method is mainly used to kill and terminate the interpreter running in the background. However, quit() method can be invoked in order to stop the process after the mainloop() function.
root. mainloop() is a method on the main window which we execute when we want to run our application. This method will loop forever, waiting for events from the user, until the user exits the program – either by closing the window, or by terminating the program with a keyboard interrupt in the console.
mainloop() i.e root. mainloop() stops. So as you just want to quit the program so you should use root. destroy() as it will it stop the mainloop() .
Creating an application using tkinter is easy but sometimes, it becomes difficult to close the window or the frame without closing it through the button on the title bar. In such cases, we can use the . destroy() method to close the window.
root.quit()
causes mainloop to exit. The interpreter is still intact, as are all the widgets. If you call this function, you can have code that executes after the call to root.mainloop()
, and that code can interact with the widgets (for example, get a value from an entry widget).
Calling root.destroy()
will destroy all the widgets and exit mainloop. Any code after the call to root.mainloop()
will run, but any attempt to access any widgets (for example, get a value from an entry widget) will fail because the widget no longer exists.
quit()
stops the TCL interpreter. This is in most cases what you want, because your Tkinter-app will also stop. It can be a problem, if you e.g. call your app from idle. idle is itself a Tkinker-app, so if you callquit()
in your app and the TCL interpreter gets terminated, idle will also terminate (or get confused ).
destroy()
just terminates the mainloop and deletes all widgets. So it seems to be safer if you call your app from another Tkinter app, or if you have multiple mainloops."
taken from http://www.daniweb.com/forums/thread66698.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With