Frames
Occasionally it might happen that the pack, grid,
or place methods of positioning widgets are not
adequate or perhaps become too complicated. In such instances
a frame may be the solution. A frame is a simple widget
whose purpose is to act as a container for other widgets
within the window.
A frame is constructed by the syntax
my $frame = $mw->Frame( [ option => value ] );
Available options described in the Tk documentation
are listed below.
- -background
This option is the same as the standard background option
except that its value may also be specified as an undefined value.
In this case, the widget will display no background or border,
and no colors will be consumed from its colormap for its background
and border.
- -colormap
This specifies a colormap to use for the window. The value may be
either ``new'', in which case a new colormap is created for the
window and its children, or the name of another window (which
must be on the same screen and have the same visual as $widget),
in which case the new window will use the colormap from the specified
window. If the colormap option is not specified, the new window uses
the same colormap as its parent. This option may not be changed with
the configure method.
- -container
The value must be a boolean. If true, it means that this window
will be used as a container in which some other application will be
embedded (for example, a Tk toplevel can be embedded using the
-use option). The window will support the appropriate
window manager protocols for things like geometry requests.
The window should not have any children of its own in this
application. This option may not be changed with the configure method.
- -height
This specifies the desired height for the window in any of the forms
acceptable to Tk_GetPixels. If this option is less than or equal
to zero then the window will not request any size at all.
- -visual
This specifies visual information for the new window in any of the
forms accepted by Tk_GetVisual. If this option is not specified,
the new window will use the same visual as its parent.
The visual option may not be modified with the configure method.
- -width
This specifies the desired width for the window in any of the
forms acceptable to Tk_GetPixels. If this option is less
than or equal to zero then the window will not request any size at all.
A label for the frame may be specified in one of two ways:
- through the use of a static label, as in
$frame = $mw->Frame( -label => 'My Frame');
- through the use of a variable label, as in
$frame = $mw->Frame( -labelVariable => $label_variable);
An example of the use of frames for placing widgets follows.
#!perl
# file frame.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Frame Example');
my $frame1 = $mw->Frame(-label => 'Frame 1',
-borderwidth => 5, -relief => 'groove');
my $frame2 = $mw->Frame(-label => 'Frame 2',
-borderwidth => 5, -relief => 'groove');
my $title = $frame1->Label(-text => 'Example of packing several widgets');
my $cb1 = $frame1->Checkbutton(-text => 'Checkbutton 1');
my $button = $frame2->Button(-text => 'A Button');
my $label = $frame2->Label(-text => 'A Label');
my $cb2 = $frame1->Checkbutton(-text => 'Checkbutton 2');
my $exit = $frame2->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
$frame1->pack;
$frame2->pack;
$title->pack;
$cb1->pack;
$button->pack;
$label->pack;
$cb2->pack;
$exit->pack;
MainLoop;
with the following screenshot illustrating the outcome.
You should compare and contrast this outcome with the use of
pack, grid, and place on these same
widgets.
Figure 2.10:
An example of the use of frames
|
Randy Kobes
2003-11-17