Before going further it is necessary to understand how instances of
widgets are named. Widgets are arranged in a hierarchy. The names of
widget instances are formed from dot separated words. The root window
. on its own. So for, example, a button widget that
is displayed in the root window might have the name
button that is displayed inside a frame that is displayed inside the
root window may have the name
.frame1.b1. The frame would have
Following this notation, it is clear that widgets are both formed in hierarchies, with the dot notation giving the path to a widget, and in groups, all widgets with the same leading path are notionally in the same group.
(It is a similar to the way file systems are organized. A file has a path that shows where to find it in the hierarchical file system. But also files with the same leading path are in the same directory/folder and so are notionally grouped together.)
An instance of a widget is created through the a Tcl command for that widget. The widget command my have optional arguments set for specifying various attributes of the widget that it will have when it is created. The result of a successful widget command is the name of the new widget.
For example, a command to create a button widget named
that displays the text “I am a button” would look like this:
button .mybutton -text "I am a button"
and this will return the name
A widget will only be created if all the windows/widgets in the leading path of the new widget also exist, and also that the name of the new widget does not already exist.
For example, the following
button .mybutton -text "I am a button" button .mybutton -text "and so am I"
will fail at the second command because there is also a widget named
.mybutton from the first command.
The following will also fail
button .frame.mybutton -text "I am a button"
if there is no existing widget with the name
.frame to be the parent
All this begs the question: why are widgets named and arranged in a hierarchy? Is not a GUI just a bunch of widgets displayed in a window?
This is not generally how GUIs are arranged. For example, they often have a menubar over the top of each window. The menubar contains pulldown menus. The pulldown menus may have cascading menu items that may cascade down several levels. Under the menu bar is the main part of the window that may also be split into several “frames”. A left hand frame my have a set of buttons in it, for example. And so on. From this you can see that the widgets in GUIs are naturally arranged in a hierarchy. To achieve this in Tcl/Tk instances of widgets are placed in a hierarchy, which is reflected in their names.
Now we will go through each of the widget commands in turn. Each widget command has many options most of which will not be described here. Just enough will be touched on for the reader to understand the basic operation of each widget. For a complete description of each widget and its many options refer to the Tk manual.