Tcl/Tk Cookbook - Basics of Tk


Quick Tour

Widget Basics

A widget is an user interface object/control (e.g., pushbutton, label, scrollbar) that the end-user of a widget based application interacts with to communicate with that application. The interaction is usually a selection made with a pointing device such as the mouse or typing in a character string (text input). Each widget belongs to a class of its own which defines its appearance (configuration options such as its foreground colour, font) and a set of methods that are used to access and manipulate the widget (e.g modify the configuration such as change the background colour).

Widgets, depending on their class/type can contain other widgets (e.g. menubars that contain pulldown menus, frame, rowcolumn). A widget based application may contain one or more hierarchy of widgets (e.g., Fileselectionbox, a text editor with a menu item "open" that pops up a fileselectionbox).

In general, there are three basic steps of widget programming. These are:

  1. create an instance of the widget (usually by calling a widget creation function). Specify values for attributes i.e.options for appearance (there will always be default settings so you only need to set the ones you want to)
  2. specify behaviour (which user actions invoke which functions)
  3. tell the geometry manager to make the widget appear on the screen in its position with respect to its parent

Note that the behaviour may be a single command such as "exit" when a "Quit" button is pressed or a set of commands with input parameters which invoke complex behaviour (e.g., selecting a button labelled "Beethovan" causes a search for a particular tape and playing it).

Widget toolkits are designed to assign the geometry management (determination of size and location relative to parent on the screen) to independent processes so that any widget can be managed by any geometry manager and multiple geometry managers coexist providing consistent behaviour (e.g., resizing the parent resizes all the children within the parents geometry).

You invoke the geometry manger, providing it with options on how you want a particular widget to be positioned (e.g. Right/left justified, placed at the top/bottom/left/right within/of its parent/siblings. If you do not specify any particular position, the geometry decides the positioning based on default algorithms.

Tk Widgets

Tk provides all the basic widget classes and there are also many contributed widgets available. Tk widget classes are distinguished by their configuration options, widget command and default bindings.

Configuration Options

Configuration options specify the appearance of the widget and what happens to the widget when the user clicks on them.

Widget Command

In Tk, when a widget is created, a unique command associated with the widget is also created. The widget command has the same name as the widget. The widget command is used to communicate with the widget to make it change its internal state - i.e. carry out actions - for instance change the background colour. For complex widgets. The actions that can be specified depend upon the class of the widget - for instance accessing, inserting, deleting items within a listbox or menu does not apply to a label widget class.

Bindings

Tk widget classes also have a set of default bindings. A binding is a general mechanism for associating a particular user action (event) with a specific application defined behaviour (e.g., Pressing the right mouse button in a particular widget pops up a help window).