[Tutor] Tkinter - master attribute

Jim Byrnes jf_byrnes at comcast.net
Tue Jun 15 22:39:27 CEST 2010


Alan Gauld wrote:
>
> "Jim Byrnes" <jf_byrnes at comcast.net> wrote in
>
>> When reading code examples I see things like
>
>> theframe.master.title('spam)
>
>> def __init__(self, master):
>> frame = Frame(master)
>
>> When I encounter these I tend to get bogged down trying to decide if
>> "master" has special meaning or is just a name the author has chosen.
>
> In the first case master is an attribute of the frame and as
> such is defined by the frame definition.
>
> In the second case master is just an arbitrary name for a
> parameter like any other. Because it is being used to correspond
> to the master attribute of the Framer(as seen in the call to Frame() )
> the author has used the name master too. But other common
> names for the same attribute are parent, root, top, etc
>
>> For example is it similar to Buttton(text='spam) where text in this
>> case has special meaning.
>
> In the first example yes, in the second no.
> Although 'text' is even more special because it is actually defined in
> the underlying Tk code rather than in Tkinter Python code.
>
>> I've goolged and found references to "widgets master attributes" but
>> nothing to really explain it. Could someone point me to a good
>> reference so that I could better understand it use.
>
> Because Tkinter is a thin wrapper around the underlying Tk tookit
> many atttributes of widgets are actually defined in the Tk code
> and simply mirrored by Tkinter. In that sense the widget attributes
> tend to have fixed names. But in Tkinter code the naming is
> essentially arbitrary and follows the usual Python naming
> conventions.
>
> HTH,
>

Alan,

Sorry it took so long for me to get back to this issue.  Thanks to you 
and Steve for your replies.

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, like the Button example I gave above.  If I want 
to 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.

Let me try another example.  The code snippet below comes from a working 
example out of a book:

class CanvasEventsDemo(canvasDraw.CanvasEventsDemo):
     def __init__(self, parent=None):
         canvasDraw.CanvasEventsDemo.__init__(self, parent)
         self.canvas.create_text(75, 8, text='Press o and r to move shapes')
         self.canvas.master.bind('<KeyPress-o>', self.onMoveOvals)
         self.canvas.master.bind('<KeyPress-r>', self.onMoveRectangles)
         self.kinds = self.create_oval_tagged, self.create_rectangle_tagged
<snip>

The word master appears only twice in the entire script so it is not 
defined somewhere else in the script.  As an experiment I changed them 
both to masterx.  When I ran the script I got the following error:

Traceback (most recent call last):
   File "canvasDraw_tags.py", line 41, in <module>
     CanvasEventsDemo()
   File "canvasDraw_tags.py", line 16, in __init__
     self.canvas.masterx.bind('<KeyPress-o>', self.onMoveOvals)
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?

Sorry to be so dense about this but I just don't get it yet.

Thanks,  Jim



More information about the Tutor mailing list