[Tutor] Tkinter

Magnus Lyckå magnus at thinkware.se
Sat May 1 11:42:06 EDT 2004

At 15:20 2004-04-29 -0500, David Rock wrote:
>* Olavi Ivask <olavi at city.ee> [2004-04-29 23:14]:
> > David Rock wrote:
> > >
> > >label = Label(root, text="Label1").grid()
> > >
> > >assigns the grid object to the variable label, while

Does the .grid() method return a "grid object"? What is a grid
object? In my experience, the .grid() method returns None, so

label = Label(...).grid()

is as meaningless as

x = [1,4,2,7,4].sort()

You really must understand the return type for each method/function
to understand these things. E.g. the Twisted networking framework
uses the practice of having many methods return "self", so that you
can write:

x = AClass().method1().method2().method3()

instead of

x = AClass()

I'm not convinced this is a good thing though...

> > >label = Label(root, text="Label1")
> > >
> > >assigns the LABEL object to label. The second one is probably better
> > >because you are working with the primary object that you can call ALL
> > >methods for, not just grid(), while the first is limited to JUST grid()
> > >
> > >
> > can i do something like that:
> > I "define" several widgets:
> >    label = Label(root, text="text")
> >    label = Label(root, text="text2")
> >    ...
> >    label.grid()
> >
> >    right now, it shows only second widget.
>No. you are overwriting the first assignment with the second. You would
>have to use label1, label2, etc. Ideally, label would be something
>vaguely descriptive so you could tell at a glance _which_ label it is
>and what it's for.

If you have plenty of labels and don't want to repeat yourself, you
can do something like:

for text in ["text", "text", ...]:
     label = Label(root, text=text)

For other controls, it's likely that you want to keep some kind
of handle to it, so that you can do things like enable/disable
etc, but with labels, we often don't care about keeping references
after we made them.

Magnus Lycka (It's really Lyck&aring;), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language 

More information about the Tutor mailing list