Tkinter: "Get root for a widget" is private?
aa8vb at yahoo.com
Wed Apr 5 12:42:58 EDT 2000
|Randall Hopper <aa8vb at yahoo.com> writes:
|> I have a timer class built around Tk timers. I realize I could just pass
|> any_old_widget into this routine, and it would store it off into state data
|> and use it periodically to access the 'after' and 'after_cancel' methods.
|> However, this timer has nothing to do with this widget in particular, so
|> why should it have a reference to it (versus the button next to it, or the
|> list widget below it)? It makes more sense for the timer class to accept
|> and bookmark the root object. This root is associated with the global Tk
|> state, and it makes sense for a timer to bookmark this to use global Tk
|> state commands.
|Unless you're doing something wacky (creating multiple roots??), you
|could just do what Tkinter itself (generally) does and use
|Tkinter._default_root to get the instance of the root object....
Right. I'm using _root() now which works. My original point wasn't that I
couldn't get root from a widget, but that to do it I violate the declared
public/private/protected policy for Tkinter. Since Python has no enforced
access control, I can poke at object internals anytime, but I really want
to avoid this.
My understanding is that .<symbol> is public, ._<symbol> is protected, and
.__<symbol> is private.
If _root() was ripped out in the next version of Python, I wouldn't have a
leg to stand on. And by all rights it's protected so removing it should
not affect me (or any other external code), unless I (or it) was deriving
off this class.
As Fredrik mentioned I could just snitch global Tk commands off
any_old_widget, but an event loop timer object that has nothing to do with
"any" widget shouldn't be keeping a handle to any_old_widget. That's not
An alternative is just to pass root around everywhere, but that seems silly
if every widget already knows its root.
So my request was for _root() to be declared public (e.g. as .root())
aa8vb at yahoo.com
More information about the Python-list