[Tutor] Tkinter - master attribute

ALAN GAULD alan.gauld at btinternet.com
Wed Jun 16 02:30:31 CEST 2010



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

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.

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.

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

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

If I get the time I will try to track this down further now that you have 
piqued my curiosity.

HTH,

Alan G.


More information about the Tutor mailing list