The place method allows one to place at some fixed
location a widget within a window. The basic usage is
$widget->place( [ option => value ] );
For the use of place, one must specify the x and
y coordinates of where in the window the widget is to be
positioned; a window's coordinates start at
(x, y) = (0, 0)
in the upper left hand corner, with x increasing to the
right and y increasing downwards.
Our basic previous
example rewritten to use place is as follows.
#!perl
# file place1.pl
use Tk;
use strict;
my $mw = MainWindow->new;
$mw->title('Place Example');
my $title = $mw->Label(-text => "Example of using place \non several widgets");
my $cb1 = $mw->Checkbutton(-text => 'Checkbutton 1');
my $button = $mw->Button(-text => 'A Button');
my $label = $mw->Label(-text => 'A Label');
my $cb2 = $mw->Checkbutton(-text => 'Checkbutton 2');
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
$title->place(-x => 30, -y => 10);
$cb1->place(-x => 1, -y => 50);
$cb2->place(-x => 100, -y => 50);
$label->place(-x => 70, -y => 90);
$exit->place(-x => 70, -y => 120);
MainLoop;
which results in the following window:
Figure 2.9:
An example of the use of place
|
As discussed in the Tk documentation,
available options to place are as follows.
- -in => $master
$master is the reference to the window relative to which $slave
is to be placed. $master must either be $slave's parent or
a descendant of $slave's parent. In addition, $master and $slave
must both be descendants of the same top-level window.
These restrictions are necessary to guarantee that $slave is
visible whenever $master is visible. If this option
isn't specified then the master defaults to $slave's parent.
- -x => location
The location specifies the x-coordinate within the
master window of the anchor point for $slave widget.
The location is specified in screen units (i.e. any of the forms
accepted by Tk_GetPixels) and need not lie within
the bounds of the master window.
- -relx => location
The location specifies the x-coordinate within the master
window of the anchor point for $slave widget. In this
case the location is specified in a relative fashion as a
floating-point number: 0.0 corresponds to the left edge of
the master and 1.0 corresponds to the right edge of the master. The
location need not be in the range 0.0-1.0. If both -x and -relx
are specified for a slave then their values are summed.
For example, -relx=>0.5, -x=-2 positions the left edge of
the slave 2 pixels to the left of the center of its master.
- -y => location
The location specifies the y-coordinate within the master
window of the anchor point for $slave widget. The
location is specified in screen units (i.e. any of
the forms accepted by Tk_GetPixels) and need not lie
within the bounds of the master window.
- -rely => location
The location specifies the y-coordinate within the master window
of the anchor point for $slave widget. In this case the value
is specified in a relative fashion as a floating-point
number: 0.0 corresponds to the top edge of the master and 1.0
corresponds to the bottom edge of the master. The location need not
be in the range 0.0-1.0. If both -y and -rely are specified for a
slave then their values are summed. For example,
-rely=>0.5, -x=>3
positions the top edge of the slave 3 pixels below the center of its master.
- -anchor => where
This specifies which point of $slave is to be positioned
at the (x, y) location selected by the -x, -y, -relx,
and -rely options.
The anchor point is in terms of the outer area of $slave, including its
border, if any. Thus, if where is ``se'',
then the lower-right corner of
$slave's border will appear at the given (x, y) location in the
master. The anchor position defaults to ``nw''.
- -width => size
The size specifies the width for $slave in screen units
(i.e. any of the forms accepted by Tk_GetPixels).
The width will be the outer width of $slave, including its border,
if any. If size is an empty string, or if no -width or -relwidth
option is specified, then the width requested internally by the window
will be used.
- -relwidth => size
The size specifies the width for $slave. In this case the width is
specified as a floating-point number relative to the width of the
master: 0.5 means $slave will be half as wide as the master, 1.0
means $slave will have the same width as the master, and so on.
If both -width and -relwidth are specified for a slave,
their values are summed. For example, -relwidth=>1.0, -width=>5 makes
the slave 5 pixels wider than the master.
- -height => size
The size specifies the height for $slave in screen units, in
any of the forms accepted by Tk_GetPixels. The height will
be the outer dimension of $slave, including its border, if any.
If the size is an empty string, or if no -height or -relheight
option is specified, then the height requested internally by
the window will be used.
- -relheight => size
The size specifies the height for $slave. In this case the
height is specified as a floating-point number relative to the
height of the master: 0.5 means $slave will be half as high as the
master, 1.0 means $slave will have the same height as the
master, and so on. If both -height and -relheight are specified
for a slave, their values are summed. For example,
-relheight=>1.0, -height=>-2 makes the slave 2 pixels
shorter than the master.
- -bordermode => mode
The mode determines the degree to which borders within the master are
used in determining the placement of the slave. The default and most
common value is ``inside''.
In this case the placer considers the area of the
master to be the innermost area of the master, inside any border: an
option of -x=>0 corresponds to an x-coordinate just inside the border
and an option of -relwidth=>1.0 means $slave will fill the area
inside the master's border. If mode is outside then the placer
considers the area of the master to include its border; this mode
is typically used when placing $slave outside its master, as with
the options -x=>0, -y=>0, -anchor=>ne. Lastly, the
mode may be specified
as ``ignore'', in which case borders are ignored: the area of the master
is considered to be its official X area, which includes any internal
border but no external border. A bordermode of ``ignore'' is probably not
very useful.
If the same value is specified separately with two different options,
such as -x and -relx, then the most recent option
is used and the older one is ignored.
Randy Kobes
2003-11-17