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

Bencsik Roland roland.bencsik at ludensis.hu
Tue Oct 17 10:03:24 CEST 2006


Michael Lange wrote:
> 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.
>>
> 
> This way, using quit() instead of destroy(), at least the windows do not disappear
> and leave you with a "ghost" process.

Hi Michael,

Although your version works well, I am still not satisfied with the
result, because the user does not know why the program is still running
when she pressed the close button.
So, currently I route the WM_DELETE_WINDOW to a warning dialog which
asks the user to use the quit method what the program offer.
Unfortunately this version also has a problem that in some cases the
warning dialog erases the already popped up dialog.

Best would be if somehow all the windows and dialogs could be closed and
the program be terminated after the clean up tasks are done.

The modified code is below:

# File: WindowClosingTest.py

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

class Gui:
     def __init__(self, owner):
         self.owner = owner
         Button(owner, text="Info", command=self.info).pack(side=LEFT)
         Button(
             owner, text="Choose directory",
             command=self.chooseDirectory
         ).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
         )

     def chooseDirectory(self):
         tkFileDialog.askdirectory(parent=self.owner)

     def deleteWindow(self):
         tkMessageBox.showwarning(
             "Quit",
             "Use 'Quit' button to end program cleanly!",
             parent=self.owner
         )

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




More information about the Tkinter-discuss mailing list