Pack

The use of pack is the most common form of managing the placement of widgets. The basic usage is
$widget->pack( [ option => value ] );
Although pack has some reasonable defaults to the options, often you will want to specify some of these explicitly for a better look. Consider the following widgets:
#!perl
# file pack1.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Pack Example');
my $title = $mw->Label(-text => 'Example of packing several widgets');
my $cb1 = $mw->Checkbutton(-text => 'Checkbutton 1');
my $button = $mw->Button(-text => 'A Button');
my $label = $mw->Label(-text => 'A Label');
my $cb2 = $mw->Checkbutton(-text => 'Checkbutton 2');
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$title->pack;
$cb1->pack;
$button->pack;
$label->pack;
$cb2->pack;
$exit->pack;
MainLoop;
which is illustrated in the figure below.

Figure 2.4: Example of the use of pack
Image pack1

When pack encounters two or more widgets that are packed without options, it arranges them according to the defaults. The order that the widgets are packed is crucial in this regard - if a widget A is packed ahead of widget B, widget A gets preference. For example, if we change the pack order in the above program from

$title->pack;
$cb1->pack;
$button->pack;
$label->pack;
$cb2->pack;
$exit->pack;
to
$exit->pack;
$button->pack;
$title->pack;
$label->pack;
$cb2->pack;
$cb1->pack;
the window appears as below.

Figure 2.5: A second example of the use of pack
Image pack2

Thus, you can change the appearance of the window by adjusting the order that widgets are packed. The appearance can also be changed by supplying pack with various options; those available, as appears in the Tk documentation, are listed below.

These options can be used to provide us with greater control over where to place the widgets; for example, the following program
#!perl
# file pack2.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Pack Example');
my $title = $mw->Label(-text => 'Example of packing several widgets');
my $cb1 = $mw->Checkbutton(-text => 'Checkbutton 1');
my $button = $mw->Button(-text => 'A Button');
my $label = $mw->Label(-text => 'A Label');
my $cb2 = $mw->Checkbutton(-text => 'Checkbutton 2');
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$exit->pack(-side => 'bottom',
            -expand => 1,
            -fill => 'x',
           );
$title->pack(-side => 'top',
             -expand => 1,
             -fill => 'x',
            );
$cb1->pack(-side => 'left');
$cb2->pack(-side => 'right');
$button->pack(-side => 'top');
$label->pack(-side => 'bottom');
MainLoop;
produces the following output:

Figure 2.6: A third example of the use of pack
Image pack3

We will see in later chapters when discussing various applications the use of some of the other pack options. As with most of programming, it is mainly through experience that a good understanding of these options will be gained.

Randy Kobes 2003-11-17