Tkinter programming problem

Eric Brunel eric.brunel at pragmadev.com
Wed Aug 6 10:35:10 EDT 2003


Kevin Burrows wrote:
> If I follow this thread correctly we are discussing how the exit from
> Python/Tkinter programs.  As it happens I have beed experimenting with
> these environments and have struck the same problem.Examples from
> "Python and Tkinter Programming" by John E Grayson.  These examples
> fire up OK but when the Quit button is hit the window freezes.  If the
> X in the window corner is hit first the application exits correctly.
> 
> Several suggestions have been suggested.  I have experimented with the
> following program:
> 
> #!/usr/local/bin/python
> from Tkinter import * # Interface to Tk widgets
> 
> class Application(Frame):
>     def __init__(self, master=None):
>         Frame.__init__(self, master)
>         self.grid()
>         self.createWidgets()
> 
>     def createWidgets(self):
>         self.quitButton = Button ( self, text="Quit",
>         command=self.quit )
>         self.quitButton.grid()
> 
>     def quit(self):
>         print sys.platform
>         print "Quitting"
>         self.quit  #--DOES NOT QET RID OF WINDOW
> #        sys.exit(0) #--DOES NOT GET RID OF WINDOW
> #        self.master.destroy() #--GETS RID OF WINDOW

This can't be the code you tested, since, again, there are no () after 
self.quit. Moreover, even if you had written self.quit(), it wouldn't have 
worked since this would call again the quit method on Application recursively, 
ending up in an exception. What you should have written is:

Frame.quit(self)

> app = Application() # Instantiate the application class
> app.master.title("Sample application")
> app.mainloop() # Wait for events
> 
> I changed commented out 2 of the 3 methods in the "quit" function
> under PythonWinIDE,  IDLE and Windows using a .pyw extension.
> 
> The results of the quit methods were:
> 
> 		PythonWinIDE 	IDLE 	 Windows using
> 					 a .pyw extension
> 
> quit		No		No	No
> 
> sys.exit		No		No	Yes
> 
> destroy		No		No	Yes
> 
> Interstingly the application does not freeze under IDLE and can still
> be closed with the windows X.
> 
> I am using Windows 98 and Python 2.2 

There always have been issues with Tkinter on Windows 98: Tkinter applications 
often freeze on exit, but not always. This is likely to be a Windows issue, 
since it works everywhere else, even on more recent version of Windows with the 
same tcl/tk version.

> So it looks like "destroy"  is the method that works.  It is
> interesting that the example and test in the Tkinter.py module use the
> "destroy" method but Grayson uses "quit" in his examples.  Perhapse
> the problem with "quit" is a MS Windows thing.

You definetely should use quit: quitting Tkinter's main loop is its intended 
use; see 
http://www.pythonware.com/library/tkinter/introduction/x9374-event-processing.htm

> I would be interested to here if the same thing happens under Unix et.
> al.

I never had any problem with the quit method on Unix (Linux or Solaris) or on 
recent version of Windows (2k, XP). I'm however always running the Python 
scripts from the command line, never from an IDE. So that may be an issue too...
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list