Tkinter programming problem

Kevin Burrows kburrows at senet.com.au
Wed Aug 6 14:53:07 CEST 2003


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
        
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 

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.

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

Kevin

On Tue, 05 Aug 2003 09:40:55 +0200, Eric Brunel
<eric.brunel at pragmadev.com> wrote:

Most deleted


>>      def quit(self, event):
>>          print"Quitting..."
>>          self.master.destroy()  # Destroy root window
>
>This may also work, but the most common way is the one I describe above. If you 
>want to do it here, you can do:
>
>def quit(self, event):
>   print "Quitting..."
>   self.master.quit()
>
>AFAIK, all Tkinter widgets have a quit method that will quit the Tk mainloop.
>
>HTH
>-- 
>- 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