Some basic questions about Tkinter (and Python)

Fredrik Lundh fredrik at effbot.org
Wed Feb 7 09:56:03 EST 2001


Martyn Quick wrote:
> I'm very new to this game and the use of lowercase and
> uppercase variants of the same English word to denote
> almost the same thing caused me problems.

Python is case-sensitive.  You better get used to it ;-)

by convention, "foo" is a variable (a named reference to
an object) or a function/method, "Foo" is a class, and
"FOO" is a constant.

(functions, methods, classes and constants are objects
too, of course)

> >     filemenu = Menu(armadillo) # create a submenu
> >     armadillo.add_cascade(label="File", menu=filemenu)
>
> Hold it - is "filemenu" a user defined name or is it built into Tkinter?

"filemenu" is just a variable name.  If you assign a value
to a name (or to be more precise, if you bind a name to
a value), you "own" that name.

(note that the Tkinter introduction assumes that you can
read Python code fluently, and that you have a basic under-
standing of Python's object and class model).

> Would the following work equally well?
>
> dongle = Menu(armadillo)
> armadillo.add_cascade(label="File", menu=dongle)

yes.

> The Introduction document (p26) says "We use the config method to attach
> it to the root window".  But I couldn't find anywhere telling me what the
> config method was.

http://www.pythonware.com/library/tkinter/introduction/widget-configuration.htm

the syntax is:

    widget.config(option=value)

Where "option" is the configuration option you want
to change, and "value" is the value you want to set it
to.  You can also set several options per call:

    widget.config(option1=value1, option2=value2, ...)

Also read up on keyword arguments (a Python feature).

> A related question is the Second Tkinter Program in that document (Hello,
> Again).  The command use to quite is  frame.quit  .  Why is this used
> instead of  sys.exit()  ?

quit causes Tkinter to return from the call to mainloop, while
sys.exit terminates the Python interpreter.  If calling mainloop
is the last thing you do in your program, it doesn't really matter
which one you use...

> Am I right in thinking that  frame  is the user defined name for
> the Frame, so if I wrote

it's a variable name (a named reference to the widget, not
necessarily the name of the Frame instance itself.  see the
section on widget names for more info)

>
> aardvark = Frame(master)
>
> I would use  command = aardvark.quit

yes.

Cheers /F







More information about the Python-list mailing list