pythonware.com products ::: library ::: search ::: daily Python-URL!

Library

An Introduction to Tkinter

Widget Configuration :::

A Django site.
   

Widget Configuration

Back   Next   

 Chapter 5. Widget Configuration

To control the appearance of a widget, you usually use options rather than method calls. Typical options include text and color, size, command callbacks, etc. To deal with options, all core widgets implement the same configuration interface:

 Configuration Interface

widgetclass(master, option=value, ...) => widget

Create an instance of this widget class, as a child to the given master, and using the given options. All options have default values, so in the simplest case, you only have to specify the master. You can even leave that out if you really want; Tkinter then uses the most recently created root window as master. Note that the name option can only be set when the widget is created.

cget(option) => string

Return the current value of an option. Both the option name, and the returned value, are strings. To get the name option, use str(widget) instead.

configure(option=value, ...), config(option=value, ...)

Set one or more options (given as keyword arguments).

Note that some options have names that are reserved words in Python (class, from, ...). To use these as keyword arguments, simply append an underscore to the option name (class_, from_, ...). Note that you cannot set the name option using this method; it can only be set when the widget is created.

For convenience, the widgets also implement a partial dictionary interface. The __setitem__ method maps to configure, while __getitem__ maps to cget. As a result, you can use the following syntax to set and query options:

    value = widget[option]
    widget[option] = value

Note that each assignment results in one call to Tk. If you wish to change multiple options, it is usually a better idea to change them with a single call to config or configure (personally, I prefer to always change options in that fashion).

The following dictionary method also works for widgets:

keys() => list

Return a list of all options that can be set for this widget. The name option is not included in this list (it cannot be queried or modified through the dictionary interface anyway, so this doesn't really matter).

Back   Next