原文:
10.3. How do I arrange the layout of my widgets?
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://www.perltk.org/contrib/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 perl/Tk build 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.
译文:
10.3. 如何安排各组件的布局?
要控制窗口中各组件的布局和外观,需要使用“布局管理器”(geometry manager),也就给每个组件设置-padding,-fill,-expand和-anchor等参数。
所谓“布局管理器”是指各种用以控制程序窗口中的组件的排列的Tk程序。在Tcl/Tk和Perl/Tk中最主要的布局管理器是pack,也被称为 packer(不太正式)。其它的管理器还有placer和canvas组件本身,但是都不太常用。另外,还有Nick Ing-Simmon的Table组件和BLT Table(这些都将在后面的问题中讨论)。迄今为止tixForm只是Tcl/Tk中有,而Perl/Tk自己的Tix仍然在编写中。(译者注:这可能有些老了,现在的管理器还有很多,诸如form,grid等等,建议读者使用perldoc参考文档)。
我们可以在组件创建的时候使用下面的方法调用pack:
$widget -> pack;
这里的widget可以是任何Perl/Tk的基本组件,而组件的参数列表通常可以使用圆括号中的关联数组(hash)来传递,如下:
$widget(-option0 => value0, -option1 => value1)->pack;
pack通常会和框架组件(frame)共同使用,从而使得窗口的安排比较有层次(基本上是长方形的排列)。下面是一个例子:
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;
注意,在这个例子中我们给pack也设置了参数。默认的pack相当于指定-side=>’top’,也可以使用在上面的例子中。
当需要细微调整组件的布局时,一个比较有用的pack的参数是设定填充:-padx或者-pady。填充的细节取决于具体使用的组件类型,而事实上,我们还可以在创建一个组件的时候就设定-pad参数。
另外,组件布局的配置还有一个-anchor的参数,它可以接受9个不同的值,具体的解释请参阅Perl/Tk安装目录中的演示程序。
当需要在一个框架中的某个组件旁边放置另一个组件时,我们可能需要在pack或创建时使用-fill => ‘sytle’选项(这里,style=none、x、y或者both)。比较典型的情况就是在一个画布(Canvas)或文本(Text)组件边上放置滚动条(Scrollbar)。
另一个需要在设置组件时考虑的问题是他们在窗口缩放时的行为(behavior),例如是否跟随窗口的边框一起变大或变小等等(具体的情况取决于窗口管理器)。这些行为可以通过在创建组件或pack的时候设置-expand选项来控制。