[Tkinter-discuss] main window closing when dialogs are open

Michael Lange klappnase at web.de
Mon Oct 16 22:11:53 CEST 2006


On Mon, 16 Oct 2006 12:42:01 +0200
Bencsik Roland <roland.bencsik at ludensis.hu> wrote:

> Hello Tkinter list,
> 
> Could you please help me out how can I make my program exit cleanly if I 
> pop up some dialog boxes?
> 
> My sample code is below.
> 
> When I execute python WindowClosingTest.py, open the dialog by pressing 
> the Info button and close the program by clicking on the close button 
> (X) of the main window than the dialog box and the main window 
> disappears but the python interpreter does not exits.
> If I close the dialog before pressing the window closing button then it 
> works fine.
> 

Hi Roland,

I changed your code a little into this:

# File: WindowClosingTest.py

from Tkinter import Tk
from Tkinter import Button
from Tkconstants import LEFT
import tkMessageBox

class Gui:
     def __init__(self, owner):
         self.owner = owner
         Button(owner, text="Info", command=self.info).pack(side=LEFT)
         Button(owner, text="Quit", command=self.quit).pack(side=LEFT)

     def quit(self):
         print "quit starts"
         print "cleaning up things..."
         self.owner.quit()
         print "quit ends"

     def info(self):
         tkMessageBox.showinfo(
             "title",
             "message",
             parent=self.owner
         )

root = Tk()
gui = Gui(root)
root.protocol("WM_DELETE_WINDOW", gui.quit)
root.mainloop()
root.destroy()
print "after mainloop"
# EOF

This way, using quit() instead of destroy(), at least the windows do not disappear
and leave you with a "ghost" process.

I hope this helps

Michael


More information about the Tkinter-discuss mailing list