Quitting a Tkinter application with confirmation

P

Peter Kleiweg

I have an application written in Tkinter. There is a menu item
'quit' that calls the function 'quit'. If 'quit' is called, it
first checks if there is unsaved data. If there is, it won't let
the application exit unless you confirm to disregard the
changes.

So far, so good.

I want the program to behave identical if the 'close' button of
the application window is clicked. I tried the code below,
using a class derived from Tk that redefines the destroy
method. That seems to work. At least on Linux.

My questions:

Is this the correct and save way to do this? Will it work on any
operating system? Shouldn't I do some event capturing instead?
(Capture the SIGTERM or SIGQUIT, sent by the Window manager to
the application.)

from Tkinter import *
import tkMessageBox

def quit():
if not changes:
root.quit()
else:
if tkMessageBox.askyesno(_('Quit'), _('Project is not saved. Ignore changes and quit?')):
root.quit()

class myTk(Tk):
def destroy(self):
quit()

root = myTk()
 
F

Fredrik Lundh

Peter said:
I want the program to behave identical if the 'close' button of
the application window is clicked. I tried the code below,
using a class derived from Tk that redefines the destroy
method. That seems to work. At least on Linux.

My questions:

Is this the correct and save way to do this? Will it work on any
operating system? Shouldn't I do some event capturing instead?

the right way to do this is to implement a WM_DELETE_WINDOW
protocol handler:

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#protocols

in your case, adding

root.protocol("WM_DELETE_WINDOW", root.destroy)

to the right place should be enough.
(Capture the SIGTERM or SIGQUIT, sent by the Window manager to
the application.)

that's signals, not events, and should not be needed. (afaik, a properly written
X Window application should shut down by itself, in response to DestroyWindow
requests from the window manager. if the window manager has to use explicit
kill signals to get rid of an application, something's wrong).

</F>
 
P

Peter Kleiweg

Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
the right way to do this is to implement a WM_DELETE_WINDOW
protocol handler:

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#protocols

in your case, adding

root.protocol("WM_DELETE_WINDOW", root.destroy)

to the right place should be enough.

Yes, that works.

By the way, this example ends the program with root.destroy().
I used root.quit(). Is there a reason for using one or the other,
or does it not matter?
 
J

jepler

Sure, there's a difference. Consider how this program behaves.
Quit only quits the mainloop, Destroy destroys the root widget.
When you Quit, you can enter the mainloop again.

from Tkinter import *
t = Tk()
Button(t, command=t.quit, text="Quit").pack()
Button(t, command=t.destroy, text="Destroy").pack()
t.mainloop()
t.mainloop()

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDe+NGJd01MZaTXX0RAu3KAKCjY0CWP4SzUu62tRbF30ZBPK5cZgCeLpFS
R/MObbYxellLtBGnDiBQzlQ=
=TKbC
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top