[ Next: BasicMapping | Previous: TkClassHier | Contents ]

A (Very) Quick Look at Tcl/Tk

This section is included because unfortunately, most of the documentation for Tkinter comes in the form of the Tk man pages and in John Ousterhout's book. To make use of this reference material, there will be times when you will need to know how to read short passages of Tk and how to identify the various parts of a Tk command. (See Mapping Basic Tk into Tkinter for the tkinter equivalents of what's below.)

Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists of tokens separated by spaces. A Tk widget is just its class, the options that help configure it, and the actions that make it do useful things.

To make a widget in Tk, the command is always of the form:

classCommand newPathname options
classCommand
denotes which kind of widget to make (a button, a label, a menu...)
newPathname
is the new name for this widget. All names in Tk must be unique. To help enforce this, widgets in Tk are named with pathnames, just like files in a file system. The top level widget, the root, is called "." (period) and children are delimited by more periods. For example, .myApp.controlPanel.okButton might be the name of a widget.
options
configure the widget's appearance and in some cases, its behavior. The options come in the form of a list of flags and values. Flags are proceeded by a `-', like unix shell command flags, and values are put in quotes if they are more than one word.
For example:

button .fred -fg red -text "hi there" ^ ^ \_____________________/ | | | class new options command widget (-opt val -opt val ...)

Once created, the pathname to the widget becomes a new command. This new widget command is the programmer's handle for getting the new widget to perform some action. In C, you'd express this as someAction(fred, someOptions), in C++, you would express this as fred.someAction(someOptions), and in Tk, you say:

.fred someAction someOptions

Note that the object name, .fred, starts with a dot.

As you'd expect, the legal values for someAction will depend on the widget's class: .fred disable works if fred is a button (fred gets greyed out), but does not work if fred is a label (disabling of labels is not supported in Tk).

The legal values of someOptions is action dependent. Some actions, like disable, require no arguments, others, like a text-entry box's delete command, would need arguments to specify what range of text to delete. (Unfortunately, John Ousterhout uses the terms "widget command" and "action" interchangeably (p 154). This usually doesn't cause much confusion in practice, but it does muddy the waters for the beginner.)