I asked that in another thread, but it got lost:

When do you use root.quit() and when do you use root.destroy() to exit a Tkinter program?

Also, can you intercept an exit when you use the little x in the title bar, just to affirm that you really want to exit, or you in case you should save data as a file before you exit.

Hi sneekula,

sorry, I totally forgot to answer in the last thread :confused:

Ok, your second question is an easy one ;)

import Tkinter as tk
import tkMessageBox

def ask_quit():
    if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
        root.destroy()

root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()

Ok, now the quit - delete one:
I didn't find anything about this in the docs. What I found was confused people who asked the same question, getting no or confusing answers.

Here's what I think:
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.

As I said, this is what I think, don't believe it, maybe I'm wrong ;)

Regards, mawe

Thanks mawe,
looks like root.destroy() would be the safer way to exit Tkinter. I don't use IDLE very often, I am a DrPython addict, but managed to freeze up IDLE a few times in the past. I checked back on that, and indeed it's associated with the use of root.quit(). Now it makes a lot more sense!

I personally always use the root.destroy() method, it seems to work best and I've never had any problems with it.

It's a good question, and my own private peace with it is that .destroy() appears to be more general: widgets and frames can be destroyed without destroying the parent, but if you .quit() anywhere within the widget tree, it appears to kill the whole toplevel.

Here's some sample code. The first button merely destroys itself; the
second button is nasty :eek:.

from Tkinter import *

# good
t1 = Tk()

t1.frame = Frame(t1)
t1.frame.button = Button(t1.frame, text="push me",\
                         command=lambda:t1.frame.button.destroy())
t1.frame.button.grid()
t1.frame.grid()

t1.mainloop()

# bad
t2 = Tk()

t2.frame = Frame(t2)
t2.frame.button = Button(t2.frame, text="push me, too",\
                         command=lambda:t2.frame.button.quit())
t2.frame.button.grid()
t2.frame.grid()

t2.mainloop()

But I could be wrong in my overall understanding...
Jeff

Thanks - this has puzzled me for years. Nice site, my first visit.

root.destroy() does the trick

With regard to the intercept and exit and asking if the user really wants to quit question, could you not use pygames event type, 'pygame.QUIT'? I'm not exactly an expert but it may work.

commented: This is six years old and you have not confirmed better solution/update! -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.