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)
> ...
The Quitter really needs to Destroy its parent, so you will need to have
something like
self.parent = parent
in the __init__() method to keep a reference to the parent frame. Then
the quit() method can call self.parent.destroy() which will also result
in the destruction of the child Quitter.
In this particular case you don't appear to need a reference to the
Quitter object in the main Frame's code, so it's acceptable to use
Quitter(self).pack()
However in the more general case yo umight want to be abel to refer to
some subsidiary object in the Frame's methods, and in that case the
easiest way to do so is to save that reference when you create the
object, than pack the obejct separately, as in
self.quitter = Quitter(self)
self.quitter.pack()
Here's a simple program to show you the difference between quit() and
destroy(). You will notice that you have to press the Quit button twice,
but the Destroy button only once - once the window is destroyed calling
its mainloop() method no longer does anything.
from Tkinter import *
t = Tk()
Button(t, command=t.quit, text="Quit").pack()
Button(t, command=t.destroy, text="Destroy").pack()
t.mainloop()
t.mainloop()
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
--
http://mail.python.org/mailman/listinfo/python-list