Grid
The grid method of placing widgets is analogous
to thinking of the window being divided up, as in a
spreadsheet, into a number of rows and columns, and
positioning a widget in a particular cell.
The basic use is as
$widget1->grid( [$widget2, $widget3, ...], [ option => value]);
which creates one row consisting of the named widgets.
Subsequent calls to grid will result in more
rows being created. For example, the following program,
#!perl
# file grid1.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Grid Example');
my $title = $mw->Label(-text => 'Example of using grid on 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->grid;
$cb1->grid($cb2);
$label->grid($button);
$exit->grid;
MainLoop;
produces the following output:
Figure 2.7:
An example of the use of grid
|
As with pack, the options available to grid
allow more control over the placement of the widgets.
The available options, as described in the Tk
documentation, are listed below.
- -column => n
Insert the $slave so that it occupies the nth column in the grid.
Column numbers start with 0. If this option is not supplied, then
the $slave is arranged just to the right of the previous slave specified
on this call to grid, or column ``0'' if it is the first slave.
For each x that immediately precedes the $slave, the column
position is incremented by one. Thus the x represents a
blank column for this row in the grid.
- -columnspan => n
Insert the slave so that it occupies n columns in the grid.
The default is one column, unless the window name is followed
by a -, in which case the columnspan is incremented once for
each immediately following -.
- -in => $other
Insert the slave(s) in the master window given by $other.
The default is the first slave's parent window.
- -ipadx => amount
The amount specifies how much horizontal internal padding
to leave on each side of the slave(s). This space is
added inside the slave(s) border. The amount must be a
valid screen distance, such as 2 or '.5c'. It defaults to 0.
- -ipady => amount
The amount specifies how much vertical internal padding to
leave on on the top and bottom of the slave(s). This space
is added inside the slave(s) border. The amount defaults to 0.
- -padx => amount
The amount specifies how much horizontal external padding to
leave on each side of the slave(s), in screen units.
The amount defaults to 0. This space is added outside the slave(s) border.
- -pady => amount
The amount specifies how much vertical external padding
to leave on the top and bottom of the slave(s), in screen units.
The amount defaults to 0. This space is added outside the slave(s) border.
- -row => n
Insert the slave so that it occupies the nth row in the grid.
Row numbers start with 0. If this option is not supplied, then
the slave is arranged on the same row as the previous slave specified
on this call to grid, or the first unoccupied row if this is the first slave.
- -rowspan => n
Insert the slave so that it occupies n rows in the grid. The
default is one row. If the next grid method contains characters
instead of $slaves that line up with the columns of this $slave,
then the rowspan of this $slave is extended by one.
- -sticky => style
If a slave's cell is larger than its requested dimensions,
this option may be used to position (or stretch) the slave
within its cell. Style is a string that contains zero or more
of the characters ``n'', ``s'', ``e'' or ``w''. The string can
optionally contain spaces or commas, but they are ignored.
Each letter refers to a side (north, south, east, or west)
that the slave will ``stick'' to. If both ``n'' and ``s'' (or ``e''
and ``w'')
are specified, the slave will be stretched to fill the entire
height (or width) of its cavity. The sticky option subsumes
the combination of -anchor and -fill that is used by pack.
The default is '', which causes the slave to be centered in
its cavity, at its requested size.
As an example, the previous program with the following
grid options,
#!perl
# file grid2.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('Grid Example');
my $title = $mw->Label(-text => 'Example of using grid on 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->grid(-columnspan => 2);
$cb1->grid($cb2, -sticky => 'w');
$label->grid($button, -columnspan => 2, -sticky => 'w');
$exit->grid(-columnspan => 2);
MainLoop;
produces the following window.
Figure 2.8:
A second example of the use of grid
|
As with pack, we will examine more of the options
to grid in the later applications.
Randy Kobes
2003-11-17