[Tutor] Tkinter class defn questions

Michael P. Reilly arcege@speakeasy.net
Sat, 7 Jul 2001 19:09:07 -0400 (EDT)


Sheila King wrote
> 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.

Frame.__init__ is an unbound method, so it needs to be passed the instance
explicitly, self.  By calling it, you are just telling the self object
to pre-initialize itself as a Frame, to then setup the Quitter values.

> 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?

Yes, that's basically correct.  The root object in this case would be
the Tkinter._default_root global object.

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

Each widget has a number of standard methods, one is mainloop, which
gets passed to the root widget.  Also, there is a module function called
"mainloop" which calls '_default_root.mainloop()'.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |