[Tutor] Tkinter class defn questions

Sheila King <sheila@thinkspot.net> Sheila King <sheila@thinkspot.net>
Tue, 10 Jul 2001 08:04:30 -0700


OK, one more thing that I thought of last night, I want to add here, to
make this topic "more complete", in case someone searches on it later.
See below...

On Mon, 09 Jul 2001 18:46:38 -0700, notme@gte.net  wrote about Re:
[Tutor] Tkinter class defn questions:

:On Sat, 07 Jul 2001 15:36:43 -0700, Sheila King <sheila@thinkspot.net>
:wrote about [Tutor] Tkinter class defn questions:
:
::
::Here is an example from the book:
[snipped]
::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)

::OK, I'm going to say what I think the code does, and then someone please
::correct me, if I am wrong.
:
:OK, here is something I didn't realize at the time I sent my previous
:message on this topic. However, I did some additional reading on Classes
:in Core Python Programming and NOW I SEE that...
:
:class NewClass(OtherClass):
:	class-suite
:
:Is how you derive a New Class from some Other Class.

Another remark about the subclassing and how it works...

Since the Quitter class is a subclass of the Tkinter Frame class, if you
do no write a __init__ function for the Quitter class, it will simply
use the Frame class' __init__ function. However, and here's the big
thing, if you DO write an __init__ function for the Quitter class, that
__init__ function will override and replace the __init__ function of the
Tkinter Frame class.

So, what if you want to take advantage of the __init__ function from the
Frame class, but embellish it a little and add to it??? In that case,
you have to declare the new __init__ function for the Quitter class, but
since this overrides the Frame's __init__, if you still want to use it,
you must call it explicitly.

That's why the statement

	Frame.__init__(self, parent)

is in the Quitter class __init__ function.

(I hope I explained this well enough...)

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