[Tutor] Tkinter class defn questions

Sheila King <sheila@thinkspot.net>, tutor@python.org Sheila King <sheila@thinkspot.net>, tutor@python.org
Mon, 09 Jul 2001 18:46:38 -0700


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:
:
:---------------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.

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.
If you want to take advantage of the __init__ procedure of the parent
class from which you are deriving the new class, then you must call the
parent class' __init__ procedure specifically, thus the

Frame.__init__(self, parent)

statement in the example above.

If I would have realized before, that this was an example of
Subclassing, I would have understood the code better in the first place!

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