473,786 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.as kyesno(_('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,e n,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 5736
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_WINDO W
protocol handler:

http://effbot.org/tkinterbook/tkinte....htm#protocols

in your case, adding

root.protocol(" WM_DELETE_WINDO W", 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_WINDO W
protocol handler:

http://effbot.org/tkinterbook/tkinte....htm#protocols

in your case, adding

root.protocol(" WM_DELETE_WINDO W", 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_WINDO W
protocol handler:

http://effbot.org/tkinterbook/tkinte....htm#protocols

in your case, adding

root.protocol(" WM_DELETE_WINDO W", 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,e n,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_WINDO W
protocol handler:

http://effbot.org/tkinterbook/tkinte....htm#protocols

in your case, adding

root.protocol(" WM_DELETE_WINDO W", 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,e n,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").pa ck()
Button(t, command=t.destr oy, text="Destroy") .pack()
t.mainloop()
t.mainloop()

Jeff

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

iD8DBQFDe+NGJd0 1MZaTXX0RAu3KAK CjY0CWP4SzUu62t RbF30ZBPK5cZgCe LpFS
R/MObbYxellLtBGnD iBQzlQ=
=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").pa ck()
Button(t, command=t.destr oy, text="Destroy") .pack()
t.mainloop()
t.mainloop()

Jeff

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

iD8DBQFDe+NGJd0 1MZaTXX0RAu3KAK CjY0CWP4SzUu62t RbF30ZBPK5cZgCe LpFS
R/MObbYxellLtBGnD iBQzlQ=
=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
7202
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 outer frame. Andrew. # Demonstration TK interface Windows application # Runs ok from within IDLE
3
7036
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 import * class App:
0
2528
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 Tray, this can make taskbar more clear. Would you please tell me how to modify my program. Thanks a lot !! Soure Code :
0
943
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 credit? Doesn't matter, low rates are fixed no matter what! Follow this link to process your application and a 24 hour approval:
0
375
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 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,
2
3845
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 the same as TKinter library is not available with JYthon. Can you please help me in porting Tkinter application to Jython? Also kindly let me know how to do the same. Thanks & Regards, Sandip Desale
4
2108
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 below, then start the program with Application(), then click the Quit button, my terminal never prints anything again (such as commands I type).
2
3960
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 asynchronous pipe to the external tool via os.popen(). Then, it dumps the output from the external process into a text display. Although threads are often recommended for use with GUI apps, I am able to keep the GUI responsive with Tkinter's event loop,...
0
1094
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 there are some wizards in here that might tell me that how it (possibly) could be done. Thomas
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.