Book HomeJava and XSLTSearch this book

21.2. 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, place, and form. pack is by far the most commonly used.

You can either pack a widget as you create it or 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;

21.2.1. 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.

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 => 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, and 0. The default is 0 (no).

-anchor => position
Anchors the widget inside the allocation rectangle. Values for positionare 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 x 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 => amount
Increases the size of the widget vertically by amount x 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 => amount
Places 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 amount
Places 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.

21.2.1.1. pack methods

The following methods are associated with widgets managed with pack:

packForget
Causes a widget to be removed from view:

$widget->packForget;

The widget is not destroyed, but it is no longer managed by pack. The widget is removed from the packing order, so if it was 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
Enables or suppresses automatic resizing of a Toplevel or Frame widget to accommodate items packed inside of it. Automatic resizing is on by default; 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;

21.2.2. 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 lefthand 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 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
Leaves 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 row specified with -row. mis any integer > 0.

-sticky => sides
Sticks widget to specified side(s). sides contains characters n, s, e, or w.

-in => $window
Grids widget inside another window instead of its parent.

-ipadx => amount
Increases the size of the widget horizontally by amount x 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 => amount
Increases the size of the widget vertically by amount x 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 => amount
Places 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 amount
Places 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.

21.2.2.1. grid methods

The following methods are associated with widgets managed by 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);
gridRemove
Removes each of the $slaves from grid for its master and unmaps their windows. The slaves will no longer be managed by the grid geometry manager. gridRemove remembers the previous configuration options for the window. So, if $slave is managed once more by the grid geometry manager, the previous values will be retained.

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);

21.2.3. 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 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 by c (centimeters), i (inches), m (millimeters), and p (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. xratiois 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 by c (centimeters), i (inches), m (millimeters), and p (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 widgets managed by place:

placeForget
Causes the widget to be removed from view.

placeInfo
Returns information about the widget.

placeSlaves
Returns a list of widgets managed by the specified parent widget.

21.2.4. The form Geometry Manager

The form geometry manager arranges the geometry of children in the parent window according to attachment rules. In addition, form can be used as a replacement for the existing Tk pack and place geometry managers. For example, to position a widget $top_w on top of $bot_w, you'd do this:

$bot_w->form(-top=>[$topw_w, 0]);

Options to form are:

-b => attachment
Abbreviation for the -bottom option.

-bottom => attachment
Specifies an attachment for the bottom edge of the slave window.

-bottomspring => attachment
Specifies the weight of the spring at the bottom edge of the slave window.

-bp => value
Abbrevation for the -padbottom option.

-bs => weight
Abbrevation for the -bottomspring option.

-fill => style
Specifies the fillings when springs are used for this widget. The value must be x, y, both, or none.

-in => $master
Places the slave window into the specified $master window. If the slave was originally in $another_master, all attachment values with respect to $original_master are discarded. The attachment values will need to be specified again. If you use -in, it must be used first in the options list.

-l => attachment
Abbreviation for the -left option.

-left => attachment
Specifies an attachment for the left edge of slave window, $slave.

-leftspring => weight
Specifies the weight of the spring at the edge of the slave window, $slave.

-lp => value
Abbreviation for the -padleft option.

-ls => weight
Abbreviation for the -leftspring option.

-padbottom => value
Specifies the amount of external padding to leave on the bottom side of the slave.

-padleft => value
Specifies the amount of external padding to leave on the left side of the slave.

-padright => value
Specifies the amount of external padding to leave on the right side of the slave.

-padtop => value
Specifies the amount of external padding to leave on the top side of the slave.

-padx => value
Specifies the amount of external padding to leave on both the left and right sides of the slave.

-pady => value
Specifies the amount of external padding to leave on both the top and bottom sides of the slave.

-r => attachment
Abbreviation for the -right option.

-right => attachment
Specifies an attachment for the right edge of the slave window.

-rightspring => weight
Specifies the weight of the spring at the right edge of the slave window.

-rp => value
Abbreviation for the -padright option.

-rs => weight
Abbreviation for the -rightspring option.

-t => attachment
Abbreviation for the -top option.

-top => attachment
Specifies an attachment for the top edge of the slave window.

-topspring => weight
Specifies the weight of the spring at the top edge of the slave window.

-tp => value
Abbreviation for the -padtop option.

-ts => weight
Abbreviation for the -topspring option.

The following methods are associated with widgets managed by form:

formCheck
Checks whether this is a circular dependancy between the master's slaves. It returns true if the dependancy exists and false otherwise.

formForget
Removes the slave from its master and unmaps its window. The slave will no longer be managed by form. All attachment values with respect to $master are discarded.

formGrid($size_x, $size_y)
Returns the number of grids to $master as integers in the form $size_x, $size_y. You may change the number of grids on the master window by passing $size_x and $size_y to formGrid.

formInfo(-option)
Queries the attachment options of a slave window. -option can be any of the options accepted by form. With no options, formInfo returns the current configuration values.

formSlaves
Returns a list of all the slaves for the master window.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.