Geometry Managers
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;
The pack Geometry Manager
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.
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 =>
side
- Puts the widget against the specified side of the window. Values for
side
are'left'
,'right'
,'top'
, and'bottom'
. The default is'top'
. -fill =>
direction
- Causes 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 =>
boolean
- Causes the allocation rectangle to fill the remaining space available in the window. Values are
'yes'
,'no'
,1
, and0
. The default is0
('no'
). -anchor =>
position
- Anchors 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
- Puts the widget after another widget in packing order.
-before =>
$widget
- Puts the widget before another widget in packing order.
-in =>
$window
- Packs the widget inside another window rather than inside its parent.
-ipadx =>
amount
- Increases the size of the widget horizontally by
amount
* 2.amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -ipady =>
amount
- Increases the size of the widget vertically by
amount
* 2.amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -padx =>
amount
- Places padding on the left and right of the widget.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -pady
amount
- Places padding on the top and bottom of the widget.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units.
Pack methods
The following methods are associated with pack
:
packForget
- Causes a widget to be removed from view.
$widget->packForget;
The widget is not destroyed, but is no longer managed bypack
. The widget is removed from the packing order, so if it were repacked later, it would appear at the end of the packing order. packInfo
- Returns a list containing all pack information about that widget.
$info = $widget->packInfo;
packPropagate
- Suppresses 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);
packSlaves
- Returns an ordered list of all the widgets packed into the parent widget.
$children = $widget->packSlaves;
The grid Geometry Manager
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
^
orx
. x
- Leave a blank space.
^
- The widget above this one (same column, previous row) should span this row.
Options to grid
are:
-column =>
n
- The column in which to place the widget.
n
is any integer >= 0. -row =>
m
- The row in which to place the widget.
m
is any integer >= 0. -columnspan =>
n
- The number of columns for the widget to span, beginning with the column specified with
-column
.n
is any integer > 0. -rowspan =>
m
- The number of rows for the widget to span, beginning with the column specified with
-row
.m
is any integer > 0. -sticky =>
sides
- Stick widget to specified side(s).
sides
contains charactersn
,s
,e
, orw
. -in =>
$window
- Grid widget inside another window instead of its parent.
-ipadx =>
amount
- Increases the size of the widget horizontally by
amount
* 2.amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -ipady =>
amount
- Increases the size of the widget vertically by
amount
* 2.amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -padx =>
amount
- Places padding on the left and right of the widget.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -pady
amount
- Places padding on the top and bottom of the widget.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units.
Grid methods
The following methods are associated with grid
:
gridColumnconfigure
- Configures 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);
gridRowconfigure
- Configures 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);
gridBbox
- Returns 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);
gridForget
- Causes the widget(s) to be removed from view. Additional widgets can be specified as arguments.
$widget1->gridForget($widget2, widget3, ...);
gridInfo
- Returns information about the widget in list format.
$widget->gridInfo;
gridLocation
- Returns the column and row of the widget nearest the specified x,y coordinates (in pixels).
$widget->gridLocation(120, 32);
gridPropagate
- Turns off automatic resizing of the widget.
$widget->gridPropagate;
gridSize
- Returns the size of the grid, i.e., the number of columns and rows.
$widget->gridSize;
gridSlaves
- Returns 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
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:
$button->place(-x => 0, -y => 0);
Options to
place
are:
-anchor =>
position
- The 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 =>
location
- Determines whether or not the border portion of the widget is included in the coordinate system. Values for
location
are'inside'
,'outside'
, and'ignore'
. -height =>
amount
- Absolute height of the widget.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -in =>
$window
- The 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 =>
ratio
- The height of the widget relates to the parent widget's height by the specified ratio.
-relwidth =>
ratio
- The width of the widget relates to the parent widget's width by the specified ratio.
-relx =>
xratio
- The 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 =>
yratio
- The 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 =>
amount
- The width of the widget will be the specified amount.
amount
can be represented as a number followed byc
(centimeters),i
(inches),m
(millimeters), andp
(printer points). Pixels are the default units. -x =>
xcoord
- The widget will be placed at the specified x coordinate.
-y =>
ycoord
- The widget will be placed at the specified y coordinate.
The following methods are associated with place
: