Creating widgets and determining how to display them are done with separate
commands. You can create a widget with one of the widget
creation methods (such as Button, Canvas, etc.),
but you display them using a geometry manager.
The three geometry managers are pack, grid,
and place. pack is by far the most commonly used.
You can either pack a widget as you create it, or you can create the widget object and pack it separately. For example, the previous "Hello World!" example might have read:
#!/usr/bin/perl -w
use Tk;
my $mw = MainWindow->new;
$button = $mw->Button(-text => "Hello World!", -command =>sub{exit});
$button->pack;
MainLoop;
With the pack geometry manager, widgets cannot overlap or
cover each other, either partially or completely. Once a
widget is packed into a window, the next widget is packed
in the remaining space around it.
pack sets up an "allocation rectangle" for each widget,
determined by the dimensions of the parent window and the
positioning of the widgets already packed into it.
This means that the order
in which you pack your widgets is very important.
By default, pack places widgets at the top center of the
allocation rectangle. However, you can use options to pack to
control where a widget is placed and how much padding is placed around it.
Options for pack are:
-side => sidePuts the widget
against the specified side of the window. Values for side are 'left', 'right', 'top', and 'bottom'. The default is 'top'.
-fill => directionCauses the widget to fill the
allocation rectangle in the specified direction.
Values for direction are 'none', 'x', 'y', and 'both'. The default is 'none'.
-expand => booleanCauses the allocation
rectangle to fill the remaining space available in the window.
Values are 'yes', 'no', 1, and 0. The default is 0 ('no').
-anchor => positionAnchors the widget inside the allocation rectangle.
Values for position are 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw',
and 'center'. The default is 'center'.
-after => $widget-before => $widget-in => $windowPacks the widget inside another window rather than inside its parent.
-ipadx => amountIncreases the size of the widget horizontally
by amount * 2.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-ipady => amountIncreases the size of the widget vertically
by amount * 2.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-padx => amountPlaces padding on the left and right of the widget.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-pady amountPlaces padding on the top and bottom of the widget.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
The following methods are associated with pack:
packForgetCauses a widget to be removed from view.
The widget is not destroyed, but is no longer managed by$widget->packForget;
pack.
The widget is removed from the packing order, so if it were repacked
later, it would appear at the end of the packing order.packInfoReturns a list containing all pack information about that widget.
$info = $widget->packInfo;
packPropagateSuppresses automatic resizing of a Toplevel or Frame widget to accommodate items packed inside of it. The following line turns off automatic resizing:
$widget->packPropagate(0);
packSlavesReturns an ordered list of all the widgets packed into the parent widget.
$children = $widget->packSlaves;
The grid geometry manager divides the window into a grid
composed of columns and rows starting at 0,0 in the upper left-hand
corner. The resulting grid resembles a spreadsheet, with each
widget assigned a cell according to the options to grid.
To create a grid, create a frame that is packed inside the parent
window and then grid the widgets within the frame.
You can specify explicit rows and columns using options to grid.
However, if several widgets are meant to appear in the same row,
you can use a single grid command with
a list of widgets rather than calling grid for each one.
The first widget invokes the grid command, and all other
widgets for that column are specified as options to grid.
Any subsequent grid commands increment the row by one and
start again.
You can use special characters as placeholders:
- (minus sign)The previous widget should span this column as well. May not
follow ^ or x.
x^The widget above this one (same column, previous row) should span this row.
Options to grid are:
-column => nThe column in which to place the widget.
n is any integer >= 0.
-row => mThe row in which to place the widget. m is any integer >= 0.
-columnspan => nThe number of columns for the widget to span, beginning with
the column specified with -column.
n is any integer > 0.
-rowspan => mThe number of rows for the widget to span, beginning
with the column specified with -row.
m is any integer > 0.
-sticky => sidesStick widget to specified side(s).
sides contains characters n, s, e, or w.
-in => $window-ipadx => amountIncreases the size of the widget horizontally
by amount * 2.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-ipady => amountIncreases the size of the widget vertically
by amount * 2.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-padx => amountPlaces padding on the left and right of the widget.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-pady
amountPlaces padding on the top and bottom of the widget.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
The following methods are associated with grid:
gridColumnconfigureConfigures the column specified by the first argument using
-weight and -minsize arguments. The -weight
argument determines the amount of space to allocate to that column,
and the -minsize argument sets the minimum size in pixels.
For example:
$widget->gridColumnconfigure(3, -weight => 1);
gridRowconfigureConfigures the row specified by the first argument using
-weight and -minsize arguments. The -weight
argument determines the amount of space to allocate to that row,
and the -minsize argument sets the minimum size in pixels.
For example:
$widget->gridRowconfigure(3, -weight => 1);
gridBboxReturns the bounding box in pixels for the space occupied by the specified grid position (in the order of column, row). For example:
$widget->gridBbox(3,2);
gridForgetCauses the widget(s) to be removed from view. Additional widgets can be specified as arguments.
$widget1->gridForget($widget2, widget3, ...);
gridInfoReturns information about the widget in list format.
$widget->gridInfo;
gridLocationReturns the column and row of the widget nearest the specified x,y coordinates (in pixels).
$widget->gridLocation(120, 32);
gridPropagateTurns off automatic resizing of the widget.
$widget->gridPropagate;
gridSizeReturns the size of the grid, i.e., the number of columns and rows.
$widget->gridSize;
gridSlavesReturns a list of all widgets contained within a master widget.
Optional -row and -column arguments restrict the
response to the widget(s) within that row or column.
$children = $widget->gridSlaves(-row => 2);
The place geometry manager lets you position a window at
explicit x,y coordinates. With place, you can overlap
widgets, which isn't allowed with grid or pack.
For example, to position a button widget at the upper left corner
of a window:
Options to$button->place(-x => 0, -y => 0);
place are:-anchor => positionThe position
in the widget that will be placed at the coordinates specified.
Values for position
are 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', and 'center'. Default is 'nw'.
-bordermode => locationDetermines whether or
not the border portion of the widget is included in the coordinate
system. Values for location are 'inside', 'outside', and 'ignore'.
-height => amountAbsolute height of the widget.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-in => $windowThe child widget will be packed inside the specified window instead of the parent that created it. Any relative coordinates or sizes will still refer to the parent.
-relheight => ratioThe height of the widget relates to the parent widget's height by the specified ratio.
-relwidth => ratioThe width of the widget relates to the parent widget's width by the specified ratio.
-relx => xratioThe widget will be placed relative to its parent by the specified
ratio. xratio is a floating point number from 0.0 to 1.0,
with 0.0 representing the left side of the parent widget and 1.0
representing the right side.
-rely => yratioThe widget will be placed relative to its parent by the specified
ratio. yratio is a floating point number from 0.0 to 1.0,
with 0.0 representing the top of the parent widget and 1.0
representing the bottom.
-width => amountThe width of the widget will be the specified amount.
amount can be represented as a number followed
by c (centimeters), i (inches), m (millimeters),
and p (printer points). Pixels are the default units.
-x => xcoord-y => ycoordThe following methods are associated with place: