Differencebetween Root.Destroy() and Root.Quit()

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

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

Difference between .quit and .destroy on tkinter

.quit() causes mainloop to exit, but doesn't directly cause any widgets to be destroyed. However, if there's no code after calling mainloop then the script exits, and all widgets will be destroyed.

.destroy() will destroy a widget. If you destroy the root window then all other widgets will be destroyed and mainloop will stop.

Why isn't root.quit() or root.destroy() working but only the combination of both?

The root of the problem is that you're calling mainloop more than once. You should not do that. It causes problems very much like the one that you're having.

If your goal is to create a popup window, and then wait for the user to dismiss the popup window, tkinter has a method specifically for that. It's called wait_window, and it won't return until the window it is waiting on has been destroyed.

top = tk.Toplevel()
popup = Popup(top, "some data", "other data")
top.wait_window(top)

You can put additional code after top.wait_window(top), and it won't execute until the popup has been destroyed.

Within your popup code, your button simply needs to call destroy on the popup window. You shouldn't call quit.

def foo(self, a, b):

#do something with widget data

self.master.destroy()

How do I close a tkinter window?

You should use destroy() to close a Tkinter window.

from Tkinter import * 
#use tkinter instead of Tkinter (small, not capital T) if it doesn't work
#as it was changed to tkinter in newer Python versions

root = Tk()
Button(root, text="Quit", command=root.destroy).pack() #button to close the window
root.mainloop()

Explanation:

root.quit()

The above line just bypasses the root.mainloop(), i.e., root.mainloop() will still be running in the background if quit() command is executed.

root.destroy()

While destroy() command vanishes out root.mainloop(), i.e., root.mainloop() stops. <window>.destroy() completely destroys and closes the window.

So, if you want to exit and close the program completely, you should use root.destroy(), as it stops the mainloop() and destroys the window and all its widgets.

But if you want to run some infinite loop and don't want to destroy your Tkinter window and want to execute some code after the root.mainloop() line, you should use root.quit(). Example:

from Tkinter import *
def quit():
global root
root.quit()

root = Tk()
while True:
Button(root, text="Quit", command=quit).pack()
root.mainloop()
#do something

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

python tkinter window don't close with root.quit() when using os.system

here is the solution if you're trying to open a command using python

your code changed root.quit() to root.destroy()
and how to execute command

os.system('cmd /k "Your Command Prompt Command"')

import tkinter as tk
import os
root = tk.Tk()
canvas = tk.Canvas()
canvas.pack()
def a():
root.destroy()
os.system('cmd /c "date"')
open_select_file = tk.Button(canvas,
text = "close",
command = a,
font=(18)
).pack()
root.mainloop()

hope helpful for you

OUTPUT of CMD

output of cmd

Using destroy in tkinter the root disappear but the script keeps running

Ok, I solved it, perhaps it helps someone in the future.

I defined a new function to quit as follows:

def _quit():
root.quit()
root.destroy()

button_exit=Button(root,text='Exit program', command=_quit)

Function to close the window in Tkinter

def quit(self):
self.root.destroy()

Add parentheses after destroy to call the method.

When you use command=self.root.destroy you pass the method to Tkinter.Button without the parentheses because you want Tkinter.Button to store the method for future calling, not to call it immediately when the button is created.

But when you define the quit method, you need to call self.root.destroy() in the body of the method because by then the method has been called.



Related Topics



Leave a reply



Submit