TKinter newbie

Discussion in 'Python' started by Gigs_, Feb 15, 2007.

  1. Gigs_

    Gigs_ Guest

    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)
    ....
     
    Gigs_, Feb 15, 2007
    #1
    1. Advertising

  2. Gigs_

    Peter Otten Guest

    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
     
    Peter Otten, Feb 15, 2007
    #2
    1. Advertising

  3. Gigs_

    Steve Holden Guest

    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
     
    Steve Holden, Feb 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    531
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    480
    Jeff Epler
    Aug 23, 2004
  3. Pierre Dagenais
    Replies:
    0
    Views:
    349
    Pierre Dagenais
    Aug 3, 2008
  4. Hidekazu IWAKI
    Replies:
    1
    Views:
    417
    Peter Otten
    Dec 14, 2009
  5. Hidekazu IWAKI
    Replies:
    0
    Views:
    552
    Hidekazu IWAKI
    Dec 15, 2009
Loading...

Share This Page