469,916 Members | 2,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Quitting a Tkinter application with confirmation


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

--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
Nov 22 '05 #1
6 5335
Peter Kleiweg wrote:
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/tkinte....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>

Nov 22 '05 #2
Peter Kleiweg wrote:
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/tkinte....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>

Nov 22 '05 #3
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
Peter Kleiweg wrote:
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/tkinte....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?
--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
Nov 22 '05 #4
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
Peter Kleiweg wrote:
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/tkinte....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?
--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
Nov 22 '05 #5
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+NGJd01MZaTXX0RAu3KAKCjY0CWP4SzUu62tRbF30 ZBPK5cZgCeLpFS
R/MObbYxellLtBGnDiBQzlQ=
=TKbC
-----END PGP SIGNATURE-----

Nov 22 '05 #6
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+NGJd01MZaTXX0RAu3KAKCjY0CWP4SzUu62tRbF30 ZBPK5cZgCeLpFS
R/MObbYxellLtBGnDiBQzlQ=
=TKbC
-----END PGP SIGNATURE-----

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Gregory | last post: by
3 posts views Thread by srijit | last post: by
reply views Thread by Peter Kleiweg | last post: by
2 posts views Thread by sandip desale | last post: by
2 posts views Thread by Kevin Walzer | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.