[Tutor] Tkinter class defn questions

Sheila King sheila@thinkspot.net
Sat, 07 Jul 2001 15:36:43 -0700


OK, I'm going through Mark Lutz' Programming Python, 2nd ed. and Chaps.
6-9 are very thorough explanations of Tkinter programming. So, I feel
like I'm doing fairly well with it (I'm on Chap. 7, right now). I even
downloaded the Python Mega-Widgets and played with those a bit.

But some of the finer details and nuances...well let's just say I don't
fully grok them, yet.

Here is an example from the book:

---------------page 305 quitter.py------------------------------
#############################################
# a quit button that verifies exit requests;
# to reuse, attach an instance to other guis
#############################################

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(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)

if __name__ == '__main__':  Quitter().mainloop()
-----------------------------------------------------------------

OK, I'm going to say what I think the code does, and then someone please
correct me, if I am wrong.

First, the __init__ function:
Takes "self" and a "parent" as parameters. 
Calls Frame.__init__(self, parent). I looked up the Frame.__init__
function in Tkinter.py and found (in part) this:


-----------------------------------------------------------------
class Frame(Widget):
    """Frame widget which may contain other widgets and can have a 3D
border."""
    def __init__(self, master=None, cnf={}, **kw):
        """Construct a frame widget with the parent MASTER.

#[much snipped]
-----------------------------------------------------------------

So, we are passing to the Frame.__init__ function, the newly
instantiated Quitter class object as "self", and so we are telling the
Frame, in effect, that "you are a new Quitter object". Is that a good
understanding of what's going on here? If the parent is "none", then it
packs on the Tk root object. Otherwise, it packs on whatever object is
passed as the parent parameter.

In the case that the program is run as the main function, this
statement:
Quitter().mainloop()

Takes no parameter for the Quitter object instantiation, so if parent is
"none", then parent is the root Tk object. And since "self" is passed to
the Frame object in Frame.__init__, then we are telling the Frame "you,
the new Quitter object, have root object as your parent", and the button
gets packed onto that.

Is this a mostly correct understanding of this code?

Can we put the .mainloop() command on any Tkinter object, not just the
root Tk object? Because as I've explained it above, the Quitter object
is not the root object. Rather, in the case the code is executed as
"main", the Quitter object is a frame on the root Tk object, with a
button on it.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/