Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between root.destroy() and root.quit()?

Tags:

python

tkinter

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?

like image 471
Gary Willoughby Avatar asked Feb 21 '10 21:02

Gary Willoughby


People also ask

What is destroy () in tkinter?

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.

What is root Mainloop () in Python?

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.

How do you break root Mainloop in Python?

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() .

How do I close a Tk window in Python?

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.


2 Answers

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.

like image 57
Bryan Oakley Avatar answered Oct 01 '22 21:10

Bryan Oakley


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 call quit() 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

like image 27
ErikT Avatar answered Oct 01 '22 20:10

ErikT