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. A label for the frame may be specified in one of two ways: An example of the use of frames for placing widgets follows.
# file
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']);
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
Image frame

Randy Kobes 2003-11-17