Navigation Logo 12.3  Frames, the Archetypal, No-Frills Widgets Navigation Logo

 

 

The frame widget maker is archetypal in the sense that what you can do with it you can do with other kinds of widget makers as well. All built-in widget makers are invoked this way:

widget_type_name  WIDGET_NAME  ?OPTIONS?
where widget_type_name is the name of a kind of widget and of the widget maker that creates such a widget. Below in Chapter 15, you see will that the section titles list all the possible widget types. WIDGET_NAME is the pathname of the new widget object as described in The Tk Way of Thinking. ?OPTIONS? is a sequence of zero or more option value pairs. All built-in widget makers return WIDGET_NAME and all of them create an object action family whose name is WIDGET_NAME. Here is the way that actions are executed.

widget_object ACTION OTHER_ARGUMENTS
Here widget_object is the same as WIDGET_NAME. With WIDGET_NAME, the context is that of a name of a potential object. With widget_object the context is that of an existing widget object.

For example, here is a command to make a frame with desired size 10 x 10 centimeters.

frame .frame -width 10c -height 10c
All widgets have a -width option. Some have a -height or -length option. These options require varying units of measurement as described above in Size. The widget command created with this widget is .frame. Here is an example of its use:
.frame configure -width 15c
which reconfigures the widget with a different width.

Frame widgets appear as rectangles – when they are visible at all. Often, frames exist to organize their slaves into something that can be properly packed in a larger widget. You may not see such a frame.

When you see a frame, it is because of a border around it or because some of it is showing with a distinctive color. All widgets can have borders of a few specific kinds. But the borders can become invisible if the -borderwidth option is set to 0. When nonzero, it is given a value in screen distances as described in the previous section. When there is a border its appearance depends on the -relief option.

One of the -relief alternatives will make a frame look like an unpushed button. Another will make a frame look like a pushed button. Indeed, it is through the -relief alternatives that buttons get their appearance. In fact, the distinctive appearance of many widgets is controlled by its borders.

Possible values for -relief are:

raised sunken flat groove ridge
Figure 12.3a shows ten frames – each possible value of -relief is shown with two border widths.

Figure 12.3a: Possible widget borders.

The "flat" value is used when you do not want a visible border. A positive value for -borderwidth with the "flat" value for -relief will produce a widget whose border takes up screen real estate even though it appears not to be there.

The frame widget has a default border width of 0. For most others, the default border width is positive.

As an example, the following creates and packs a 5 centimeter square frame with a two pixel border that will make it look like a button.

pack [frame .fake_button -relief raised -width 5c -height 5c]

Exercise 12.3a

Reproduce Figure 12.3a. Use 4 and 8 as the two borderwidths for each frame.

Solution

The widget command that is created with a frame is as simple as can be. There are only two actions. These actions are available to all widgets created by built-in widget makers. They are:

widget_object configure ?OPTIONS?
This changes the option values for an existing widget object. ?OPTIONS? is a sequence of option/value pairs. For example,
.frame configure -height 5c -width 5c
When ?OPTIONS? is missing, the command returns a list of all option/value pairs for the widget.

widget_object cget OPTION_NAME
This returns the value for the named option. When values measured in screen distances are returned, the actual unit is always pixels – even if the option was declared with inches, millimenters, centimeters, or printer's points.

One example of the configure action was shown above. Here is another applied to a widget named .p.

.p configure -relief groove -borderwidth 2m
This will change the widget's relief to "groove" and make that groove 2 millimeters wide. If .p is mapped to the screen, the change will be visible almost immediately. (What actually happens is that a command to make the change goes immediately on the event loop. Usually, that means you see the change immediately on the screen.)

For some widgets there are a few options that cannot be changed with the widget command's configure action – they are fixed forevermore when the widget is created. If you have trouble changing an option, check your on-line manual to see if this is the reason.

Notice that both the configure action and the cget action can be used to query option values. For example, this command,

.p cget -width 
will tell you the desired width of the widget named .p and this command

 
.p configure
will give you a list of all .ps options and their values.

Exercise 12.3b

This command
pack [frame .f -width 15m -height 15m -relief groove]
will produce a frame in your root window that has no visible groove. Why? How can the cget action verify your answer?

Solution

Remark

The differences between widget types lie in their default options and the event handlers associated with them. They normally look different because of the defaults. They behave differently because of the event handlers.

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.