If you send only a row or column number, a list of key/value pairs is
returned with the current options and their values for that method:
@column_configs = $mw->gridColumnconfigure(0);
@row_configs = $mw->gridRowconfigure(0);
Depending on your sensibilities, you may want to store the results in
a hash:
%column_configs = $mw->gridColumnconfigure(0);
%row_configs = $mw->gridRowconfigure(0);
In this example, we are getting the options and their values for the
first column and the first row. The results of using the default
values would look like this:
-minsize 0 -pad 0 -weight 0
-minsize 0 -pad 0 -weight 0
You can get the value of only one of the options by sending that
option as the second argument:
print $mw->gridColumnconfigure(0, -weight), "\n";
print $mw->gridRowconfigure(0, -weight), "\n";
The results would be:
0
0
To change the value of the option, use the option followed
immediately by the value you want associated with it. For example:
$mw->gridColumnconfigure(0, -weight => 1);
$mw->gridRowconfigure(0, -weight => 1);
You can also specify multiple options in one call:
$mw->gridColumnconfigure(0, -weight => 1, -pad => 10);
$mw->gridRowconfigure(0, -weight => 1, -pad => 10);
Now that we know how to call gridColumnconfigure
and gridRowconfigure, we need to know what the
three different options do.
2.2.8.1. Weight
The -weight option
sets the amount of space allocated to the column or row when the
window is divided into cells. Remember to use
-sticky =>
"nsew" in your grid command if
you want the widget to resize when the cell does. The default
-weight is 0, which causes the column width or row
height to be dictated by the largest widget in the column. Each
-weight value has a relationship to the other
-weight s in the rows or columns.
If a column or row has a -weight of 2, it is twice
as big as a column or row that has a -weight of 1.
Columns or rows of -weight 0 don't get
resized at all. If you want all your widgets to resize in proportion
to the size of the window, add this to your code before you call
MainLoop:
($columns, $rows) = $mw->gridSize( );
for ($i = 0; $i < $columns; $i++) {
$mw->gridColumnconfigure($i, -weight => 1);
}
for ($i = 0; $i < $rows; $i++) {
$mw->gridRowconfigure($i, -weight => 1);
}
This code will assign the -weight of 1 to every
single row and column in the grid, no matter what size the grid is.
Of course, this example works only if you want to assign the same
size to each row and each column, but you get the idea.
Here is an example of how the -weight option works
(Figure 2-36 shows the result):
$mw->Button(-text => "Button1", -command => sub { exit })->grid
($mw->Button(-text => "Button2", -command => sub { exit }),
$mw->Button(-text => "Button3", -command => sub { exit }),
$mw->Button(-text => "Button4", -command => sub { exit }),
-sticky => "nsew");
$mw->Button(-text => "Button5", -command => sub { exit })->grid
("x",
$mw->Button(-text => "Button7", -command => sub { exit }),
$mw->Button(-text => "Button8", -command => sub { exit }),
-sticky => "nsew");
$mw->gridColumnconfigure(1, -weight => 1);
$mw->gridRowconfigure(1, -weight => 1);
By giving row 1 and column 1 weights of 1 (whereas all other rows and
columns have 0 weights), they take over any extra available space
when the size of the window is increased. Notice that columns 0, 2,
and 3 are only as wide as is necessary to draw the Buttons and their
text, but column 1 has filled in the extra space. The same effect
happens for row 0 with a weight of 0 and row 1 with a new weight of
1. (The window has been resized larger to demonstrate the effects of
-weight.)
Figure 2-36. gridRowconfigure and gridColumnconfigure example