[Tutor] Tkinter - master attribute
jf_byrnes at comcast.net
Thu Jun 17 04:35:07 CEST 2010
ALAN GAULD wrote:
>> I still am having trouble understanding the use of "master" in
>> Tkinter. I think the problem is I can't find any reference that explains the
>> concept around master,
> If you read the GUI topic in my tutorial it explains the concept
> of a containment tree that is common to ost GUI frameworks
> including Tkinter. All widgets belong to a parent or master
> widget until you get to some kind of root or main window that
> is the master odf all its sub widgets.
I'll make it my next stop.
> When you delete a window you delete the master and it deletes
> all its children. The children delete their children, and so on until
> all the widgets making up the window are deleted. Thats how GUIs work.
> Similarly if you add a widget to a window you must tell the new
> widget where within the containment tree it sits, you tell it who
> its master is. Usually the widget will register itself with its master.
>> put the word spam on a Button I found a reference that said you type the word
>> text followed by an equal sign followed by spam in quotes.
> That's slightly different in that text is an attribute of the widget itself.
> By assigning 'Spam' to the attribute you control the look of that particular
> Button widget.
OK. That was the best example I could come up with to convey my
confusion concerning master. I could look at a reference and see that
in that context the word text had a special purpose, I couldn't do that
> class CanvasEventsDemo(canvasDraw.CanvasEventsDemo):
> def __init__(self, parent=None):
> canvasDraw.CanvasEventsDemo.__init__(self, parent)
> Here parent is being used instead of master. Thats quite common.
Its usage like this that tends to confuse me. If I change all
occurrences of of parent to Xparent the program runs. If I replace
parent with master the program runs. If I replace canvas.master with
canvas.xmaster I get an error.
>> self.canvas.master.bind('<KeyPress-o>', self.onMoveOvals)
> Here the master attribute is being accessed. That is an attribute
> of the canvas widget.
>> word master appears only twice in the entire script so it is not defined
>> somewhere else in the script.
> It is an inherited attribute and is inherited by all widgets
> although from where is a little bit mysterious - see below...
>> AttributeError: Canvas instance has no attribute
>> So the Canvas does not have a masterx attribute but does have
>> one called master. Maybe the bottom line question is where can I look to
>> see a list of a widgets attributes?
> You can use dir() or help().
> However in Tkinter it is further complicated by the mapping of Tkinter
> to the underlying Tk widgets. It is not always 1-1 and some attributes
> are implemented by the Tk tookit rather than at the Tkinter level and
> these do not always show up in dir(). Master appears to be one of these.
> However, on digging a little deeper it seems there is a subtle distinction
> in Tkinter between the containment hierarchy and the geometry manager
> hierarchy. I confess that I've never noticed this before and have only
> skimmed the material(Grayson's Tkinter book) but it seems that master
> refers to the GM hierarchy and parent to the containment one. In most
> cases they will be the same but they can be different. But because of
> this master seems to be implemented somewhere in the GM code
> rather than the widget code...
> In most cases you can ignore that - as I have been doing for the last 10 years! :-)
> Just use master as an inherited attribute that is present in all widgets.
OK, good. This is what I was looking for, an explicit statement of what
>> Sorry to be so dense about this but I just don't get it yet.
> You are not being dense but asking rather deep questions which
> probably need someone who understands the Tkinter implementatioon
> to answer. You probably don't really need to know the detail, just accept
> that master will be the attribute and it will be there in each widget class
> you use.
I really wasn't trying to delve deep into the innards of Tkinter, it
just seemed like master was appearing like magic and I just wanted to
nail it down so I could get on learning Python.
> If I get the time I will try to track this down further now that you have
> piqued my curiosity.
It does, I finally feel like I have a handle on it. Thanks for your time
> Alan G.
More information about the Tutor