473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 5665
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Gregory | last post by:
Could someone help me out with these few lines of code: I would like to know why the Quit button in this application removes the buttons and causes "Quitting" to be printed, but does not close the...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
0
by: ³Â Ñô | last post by:
I write a program with Python 2.4 + Tkinter Execute it, there will be a window show something. If I minimize it, it will be minimized to the taskbar. But I would like it to miniminze to the System...
0
by: Wxlu.Estes | last post by:
Hello, We sent you an email a while ago, because you now qualify for a much lower rate based on the biggest rate drop in years. You can now get $325,000 for as little as $615 a month! Bad...
0
by: Peter Kleiweg | last post by:
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...
2
by: sandip desale | last post by:
Dear All, We have a Tcl/Tk application written using Python 2.2. Using this application we want to call some customizable Java APIs. I tried porting Tcl/Tk application to Jython but not able to do...
4
by: Chris | last post by:
Hi, I'm puzzled by some strange behavior when my Python/Tkinter application quits (on linux): the terminal from which I started Python is messed up. If start up python, then import the code...
2
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
0
by: Thomas Ploch | last post by:
Hello folks, I already found some answers on the net, which said that the Tk library that Tkinter wraps does not offer functionality to minimize an application to the system tray. But I hope...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.