Tkinter programming problem

Eric Brunel eric.brunel at pragmadev.com
Tue Aug 5 09:40:55 CEST 2003


klappnase wrote:
>>
>># Demonstration TK interface Windows application
>># Runs ok from within IDLE
>>#
>>from Tkinter import *
>>
>>class CommonStuff:   # to get common access to variables and functions
>>    def __init__(self, frame):
>>        self.frame = frame
>>
>>    def say_hi(self):
>>        print "Hello all"
>>
>>
>>class MyWidgets(Frame, CommonStuff):
>>    def __init__(self, CS):
>>        Frame.__init__(self, CS.frame)
>>        self.quitbutton = Button(self, text='Quit', fg='red',
>>command=self.destroy)
>>        self.quitbutton.pack(side=LEFT)
>>        self.hi_there = Button(self, text='Hello', command=CS.say_hi)
>>        self.hi_there.pack(side=LEFT)
>>
>>
>>class Application:
>>    def __init__(self, master):
>>        self.frame=Frame(master)
>>        CS = CommonStuff(self.frame)
>>
>>        displayedwidget=MyWidgets(CS)
>>        displayedwidget.grid(row=0, column=0)
>>        self.frame.grid(row=0, column=0)
>>        displayedwidget.bind("<Destroy>", self.quit)
>>        self.frame.update()
>>        
>>    def quit(self, event):
>>        print"Quitting..."
>>        self.frame.destroy()  # Destroy frame and all children
>>
>>        
>>root = Tk()
>>mainWin = Application(root)
>>root.mainloop()
> 
> 
> I think you could have it easier, if you just want to exit you
> application with the quit button:
> 
> class MyWidgets(Frame, CommonStuff):
>      def __init__(self, CS):
>          Frame.__init__(self, CS.frame)
>          self.quitbutton = Button(self, text='Quit', fg='red',
>                                    command=self.quit)
>          self.quitbutton.pack(side=LEFT)
>          self.hi_there = Button(self, text='Hello', command=CS.say_hi)
>          self.hi_there.pack(side=LEFT)
> 
>      def quit(self):
>          print "Quitting..."
>          sys.exit(0)

This works, but actuallly quits the application immediatly. So if you happen to 
have code after the call to the Tk mainloop, it won't be executed, which might 
not be what you want. Moreover, there's already a quit method defined on 
MyWidgets, inherited from Frame, that does exactly what you want: quitting the 
Tk mainloop. So you could just do:

def quit(self):
   print "Quitting..."
   Frame.quit(self)

> If you want to run it from within the interpreter (I am not sure if it
> is that what you are trying) the following might work too:
> 
>  class Application:
>      def __init__(self, master):
>          self.frame=Frame(master)
>          CS = CommonStuff(self.frame)
>  
>          displayedwidget=MyWidgets(CS)
>          displayedwidget.grid(row=0, column=0)
>          self.frame.grid(row=0, column=0)
>          displayedwidget.bind("<Destroy>", self.quit)
>          self.frame.update()
>          
>          self.master = master
> 
>      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