Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 25.  The Place Geometry Managery

place Basics

The place command lets you specify the width and height of a window, and the X and Y locations of the window's anchor point. The size and location can be specified in absolute or relative terms. Relative specifications are more powerful. Example 25-1 uses place to center a window in its parent. You can use this command to position dialogs that you do not want to be detached top-level windows:

Example 25-1 Centering a window with place.
place $w -in $parent -relx 0.5 -rely 0.5 -anchor center

The -relx and -rely specify the relative X and Y positions of the anchor point of the widget $w in $parent. A relative X (or Y) value of zero corresponds to the left (or top) edge of $parent. A value of one corresponds to the right (or bottom) edge of $parent. A value of 0.5 specifies the middle. The anchor point determines what point in $w is positioned according to the specifications. In Example 25-1 the center anchor point is used so that the center of $w is centered in $parent.

The relative height and width settings are used to base a widget's size on another widget. Example 25-2 completely covers one window with another window. It uses the default anchor point for windows, which is their upper-left hand corner (nw):

Example 25-2 Covering a window with place.
place $w -in $parent -relwidth 1 -relheight 1 -x 0 -y 0

The absolute and relative size and position parameters are additive (e.g., -width and -relwidth). You can make a window slightly larger or smaller than the parent by specifying both parameters. In Example 25-3, a negative width and height are used to make a window smaller than another one:

Example 25-3 Combining relative and absolute sizes.
place $w -in $parent -relwidth 1 -relheight 1 -x 0 -y 0 \
   -width -4 -height -4

It is not necessary for $parent to actually be the parent widget of $w. The requirement is that $parent be the parent, or a descendant of the parent, of $w. It also has to be in the same top-level window. This guarantees that $w is visible whenever $parent is visible. These are the same restrictions imposed by the pack geometry manager.

It is not necessary to position a widget inside another widget, either. Example 25-4 positions a window five pixels above a sibling widget. If $sibling is repositioned, then $w moves with it. This approach is useful when you decorate a resizable window by placing other widgets at its corners or edges. When the window is resized, the decorations automatically move into place:

Example 25-4 Positioning a window above a sibling with place.
place $w -in $sibling -relx 0.5 -y -5 -anchor s \
   -bordermode outside

The -bordermode outside option is specified so that any decorative border in $sibling is ignored when positioning $w. In this case the position is relative to the outside edge of $sibling. By default, the border is taken into account to make it easy to position widgets inside their parent's border.

The parent widget does not have to be a frame. Example 25-1 can be used to place a dialog in the middle of a text widget. In Example 25-4, $sibling and $w can both be label widgets.

      Previous section   Next section