TKinter newbie

Peter Otten __peter__ at web.de
Thu Feb 15 13:24:44 EST 2007


Gigs_ wrote:

> Hi Im new to gui programming
> 
> from Tkinter import *                          # get widget classes
> from tkMessageBox import askokcancel           # get canned std dialog
> 
> class Quitter(Frame):                          # subclass our GUI
>      def __init__(self, parent=None):           # constructor method
>          Frame.__init__(self, parent)
>          self.pack()
>          widget = Button(self, text='Quit', command=self.quit)
>          widget.pack(side=LEFT)
>      def quit(self):
>          ans = askokcancel('Verify exit', "Really quit?")
>          if ans: Frame.quit(self)
> 
> class Demo(Frame):
>      def __init__(self, parent=None):
>          Frame.__init__(self, parent)
>          self.pack()
>          Label(self, text="Basic demos").pack()
>          for (key, value) in demos.items():
>              func = (lambda key=key: self.printit(key))
>              Button(self, text=key, command=func).pack(side=TOP,
>              fill=BOTH)
>          Quitter(self).pack()    # here
>      def printit(self, name):
>          print name, 'returns =>', demos[name]()
> 
> 
> My problem is in class Demo. How is the best way to use class Quitter in
> class Demo?
> should it be:
> Quitter(self).pack()
> Quitter(self)
> ...

You are calling the Quitter's pack() method twice, once in
Quitter.__init__() and then again in Demo.__init__(). I would remove the
call in Quitter.__init__(). If you do that you can use your Quitter class
with other layout managers which require other configuration methods, e. g.
Quitter(...).grid(...).

Peter




More information about the Python-list mailing list