To control the layout and appearance of widgets in a window one makes use of a geometry manager, as well as -padding, -fill, -expand, and -anchor options of individual widgets.
A geometry manager is any Tk procedure for controlling the arrangement of widgets in your application window. The predominant geometry manager used in both Tcl/Tk and perl/Tk is pack also known informally as the "packer" (other geometry managers are the "placer" and the canvas widget itself but are much less popular. There is also Nick Ing-Simmon's Table widget [discussed in a later question] and BLT_Table [which made it's way into perl/Tk thanks to Guy Decoux - but is also discussed in a later question]. So far tixForm is for Tcl/Tk only, but a perl/Tk version of Tix is in the works. You can invoke pack at the time of widget creation via calls like:
$widget->pack;where widget can be any of the perl/Tk widget primitives. Widget option lists are usually passed as an associative array (hash) in parentheses thusly:
$widget(-option0 => value0,-option1 => value1)->pack;pack is often used in conjunction with the frame container widget to arrange your widgets much like a hiearchically arranged set of window panes (ultimately in a rectangular "tiling" fashion of sorts). An example of this would be:
my $top2 = $main->Toplevel; my $frame = $top2->Frame; $frame->pack; $frame->Label(-text => 'Left2')->pack(-side => 'left'); $frame->Label(-text => 'Right2')->pack(-side => 'right'); $top2->Label(-text => 'Bottom2')->pack(-side => 'bottom'); MainLoop;Note that pack itself is given parameters in this example. The default behavior for pack is equivalent to specifying -side => 'top' which can be overridden as in the above example.
(Full source code for this and other examples from UserGuide.pod may be found at http://w4.lns.cornell.edu/~pvhp/ptk/pod/. To load code from the web save as a local file say ex2.pl, edit the first line to point to your perl interpreter, change permission using: chmod u+x ex2.pl, then type the name of your script: ex2.pl.)
One of the more helpful options to pass to pack when trying to get a given widget layout "just right" is through padding: either -padx or -pady. The details of the use of pad depend on which specific widget you are trying to pack. In fact you can often add the -pad in the call to create the widget rather than in the call to pack.
There is also the -anchor configuration option for widgets. A good introduction to the 9 possible -anchor (and -overanchor) values is given by the popup demo in your Tk-b#/ directory.
When setting a widget within a frame next to another widget one may wish to make use of the -fill => 'style' (where style = none | x | y | both) options of either pack or the widget itself. A typical situation where this is used is in setting up the Scrollbar next to a Canvas or Text widget.
Another aspect to consider when laying out your widgets is their behavior under resize operations (grabbing a part of the window frame and making it bigger or smaller - details depend on your window manager). This may be controlled by the -expand option of either pack or the widget itself.
Previous | Return to table of contents | Next