分享
 
 
 

Perl/TkFAQ-10.3.如何安排各组件的布局

王朝perl·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

原文:

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选项来控制。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有