An Introduction to Tkinter | ||
Copyright © 1997 by Fredrik Lundh <[email protected]> | ||
Updated 10 Aug 1997 |
< Tkinter Classes | Widget Configuration | Option Values >
To control the appearance of a widget, you usually use options rather than method calls. Typical options include text and colour, size, command callbacks, etc. To deal with options, all core widgets implement the same configuration interface:
widgetclass (master, option=value, ...). 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. Note that the name option can only be set when the widget is created.
cget(option). 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.
config(). Same as configure.
configure(option=value, ...). Set one or more options (given as keyword arguments). The name option cannot be set by this method. 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(). 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).
Keyword arguments were introduced in Python 1.3. Before that, options were passed to the widget constructors and configure methods using ordinary Python dictionaries. The source code could then look something like this:
self.button = Button(frame, {"text": "QUIT", "fg": "red", "command": frame.quit})
self.button.pack({"side": LEFT})
Admittedly, the keyword argument syntax is much more elegant, and less error prone. However, for compatibility with existing code, Tkinter still supports the older syntax. New programs should not use this syntax, even if it might be tempting in some cases. For example, if you create a custom widget which needs to pass configuration options along to its parent class, you may come up with something like:
def __init__(self, master, **kw): Canvas.__init__(self, master, kw) #
kw is a dictionary
This works just fine with the current version of Tkinter, but it may not work with future versions. A more general approach is to use the apply function:
def __init__(self, master, **kw):
apply(Canvas.__init__, (self, master), kw)
The apply function takes a function (an unbound method, in this case), a tuple with arguments (which must include self since we're calling an unbound method), and optionally, a dictionary which provides the keyword arguments.
< Tkinter Classes | Widget Configuration | Option Values >