-->
Page 518
To create and manipulate widgets, the windowing shell, wish, must be used. To invoke wish interactively, type wish at the UNIX prompt. The following wish prompt will appear:
%
Along with this, an empty window will pop up on the screen. This window is the wish root window (called .) and all the widgets that are created will appear with it.
This section shows how to create a widget and manipulate it. First, create a button:
button .button;
So what did that do?
Well, the widget type is specified as button, so tk created a button. The path is .button, so tk created the button in the root window (. is the root tk window) and named it button.
So where is the button, anyway?
The button isn't displayed right now; tk simply created it. In order to display the button, you need to tell tk how to display the widget. For this, use the pack command and give it the path to the widget you want to display:
pack .button;
Now the button is showing, but it's blank (see Figure 25.1). This is where widget's options come into play.
Page 519
All tk widgets use standard options that control appearance and function. Most widgets understand the following options:
-background color, -bg color | The background color of the widget. Valid values are of the form #RRGGBB, #RRRGGGBBB, or one of the names defined in /usr/lib/X11/rgb.txt. |
-foreground color, -fg color | The foreground color of the widget. Valid values are of the form #RRGGBB, #RRRGGGBBB, or one of the names defined in /usr/lib/X11/rgb.txt. |
-height pixels | The widget's height in pixels. |
-width pixels | The widget's width in pixels. |
-borderwidth pixels, -db pixels | The width of the widget's border in pixels. |
-padx pixels | Extra space required by the widget in the x direction. |
-pady pixels | Extra space required by the widget in the y direction. |
-relief type | The 3D effect of the widget, where type is one of these strings: flat, raised, grove, ridge, sunken. |
-text string | The string to display in the widget. |
-font font | The font to be used for the text displayed in a widget; valid font definitions are given by the command xlsfonts. |
-command command | The tcl command to execute when the widget is used; usually this is the name of a procedure or an exec statement. |
In addition to these options, the pack command understands the following options of its own:
-side type | Controls the order in which widgets are placed. Valid types are left, right, top, or bottom. For example, left indicates that new widgets should placed to the left of existing widgets. |
-fill type | Controls whether or not widgets are stretched to fill up open space in the window. Valid values are none, x, y, or both. For example, both indicates that widgets should fill up all open space. |
-expand value | Controls whether or not widgets expand if the window's size increases. value is either 0 or 1, with 1 indicating true. |
Page 520
Now that you know about the options for widgets and for pack, you can start using them. One of the interesting features of widgets is their reliefs, the widgets' 3D look. To get an idea of how each relief looks, make some labels, using the following:
foreach i {raised sunken flat groove ridge} { label .$i -relief $i -text $i; pack .$i }
This example iterates through the set of relief types, creating one label for each type, along with setting each label's text to be the relief type. The layout will look similar to Figure 25.2.
Figure 25.2.
Labels of varying
reliefs.
There are two things to notice here. First, the labels are not all the same size. Second, the labels are stacked one on top of the other. This is an example of the pack command's default behavior; it determines the size of each widget automatically and then places each widget below the preceding widget that was placed.
Let's make all the labels the same size and pack them next to each other, instead of one on top of the other. There are two ways to do this. The first is to rewrite the loop:
foreach i {raised sunken flat groove ridge} { label .$i -relief $i -text $i -height 10 -width 10; pack .$i -side left }
The second way is to reconfigure the labels using the configure option, which has the following syntax:
widget configure option
Page 521
In this case, you could use the following loop (after the labels are created):
foreach i {raised sunken flat groove ridge} { .$i configure -height 10 -width 10; pack .$i -side left; }
So why use configure?
If wish is run interactively, and one version of the loop was given, modifying it and running it again will produce the following error:
window name "raised" already exists in parent
This is the method in which wish tells the programmer that the program has attempted to re-create an existing widget (in this case with the label raised). So, you need to use configure. In fact, configure is required any time an existing widget needs to be changed.
In this case, the only way to use the new version of the loop is to destroy the existing labels, using the destroy command:
foreach i {raised sunken flat groove ridge} { destroy .$i }
The new result will be similar to Figure 25.3.
Figure 25.3.
Labels of varying
relief, packed next to
each other.
Now back to the example. There are two things that look like they should be fixed in Figure 25.3. First, it is difficult to tell the labels apart. Second, most of the window is blank.
You can make the labels easier to distinguish by padding them when they are packed and by increasing their borderwidths. To make the labels take up all of the available space, give pack the fill option for both x and y and set the expand option to true: