Tcl/Tk:调整大小时帧行为异常. [英] Tcl/Tk: Frames misbehave when resizing.

查看:33
本文介绍了Tcl/Tk:调整大小时帧行为异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Tcl/Tk 比较陌生,但在过去的几周里一直在相当成功地使用它,不停地工作.在大多数情况下,它看起来相当简单.但是,我最近遇到了一个障碍,这可能很简单,但我似乎无法弄清楚.

I am relatively new to Tcl/Tk but have been working with it fairly successfully, non-stop for the last several weeks. For the most part it seems fairly straightforward. However, I've hit a snag recently which is probably simple but I can't seem to figure it out.

我已将相当复杂的代码简化为几行来演示问题.

I have reduced my fairly complicated code to a few lines which demonstrate the problem.

我的简单示例生成两个标签框,每个标签框带有一个简单的标签小部件.当我调整(即扩展)主窗口的大小时,我希望左"框架仅在 y 方向上调整大小,而我希望中间"框架在 x 和 y 方向上调整大小.这些部分工作正常.

My simple example produces two labelframes with a simple label widget in each. When I resize (i.e., expand) the main window, I want the "left" frame to resize in the y-direction only and I want the "middle" frame to resize in x and y. Those parts work fine.

然而,我也希望框架在调整大小时保持粘在一起".也就是说,当中间"框架都调整大小时,我希望中间"框架保持贴靠左"框架.相反,我看到两者之间的差距在扩大.

However, I also want the frames to stay "stuck" to each other when resized. That is, I want the "middle" frame to stay stuck up against the "left" frame when they both resize. Instead, I see a gap expanding between the two.

同样,这是来自更复杂程序的简化代码.我试图避免在同一个程序中混合使用 grid 和 pack 命令,并且在原来更复杂的程序中,使用 grid 变得非常笨拙.因此,首选使用pack"的解决方案.但适当的解释将是一个好的开始.

Again, this is a simplified bit of code from a more complicated program. I am trying to avoid mixing grid and pack commands in the same program and, in the original more complicated program, using grid became very unwieldy. So a solution using "pack" would be preferred. But proper explanation would be a good start.

我的小例子:

 #!/usr/bin/wish

 ## Set up two main frames
 set frameLeft [labelframe .frameLeft]
 set frameMiddle [labelframe .frameMiddle]

 pack $frameLeft -fill y -expand 1
 pack $frameMiddle  -fill both -expand 1 
 pack $frameLeft $frameMiddle -side left -anchor nw

 ## Instead of the previous 3 lines, I also tried:
 #pack $frameLeft -fill y -expand 1 -side left -anchor nw
 #pack $frameMiddle  -fill both -expand 1 -side left -anchor nw

 ## Set up component in LEFT frame
 set frameOne [labelframe $frameLeft.frameOne]
 set labelOne [label $frameOne.labelOne -text "Label One:"]

 pack $labelOne -anchor nw
 pack $frameOne -anchor nw

 ## Set up component in MIDDLE frame
 set frameTwo [labelframe $frameMiddle.frameTwo]
 set labelTwo [label $frameTwo.labelTwo -text "Label Two:"]

 pack $labelTwo -side left
 pack $frameTwo -anchor nw

推荐答案

首先,在调试复杂的布局时,在框架上放置花哨的颜色通常会有所帮助.当然,您不会将它们留在生产环境中,但它确实可以让您更轻松地了解您正在使用的几何管理器的行为.

First off, it usually helps when debugging a complex layout to put garish colors on the frames. Sure, you won't leave them in production, but it does make understanding the behavior of the geometry manager you're using much simpler.

当我尝试您的代码时,我看到两个小部件都在水平方向上分配了相同的空间;.frameLeft 小部件然后粘在该空间的左侧,而 .frameMiddle 小部件正在填充它分配的空间.因此,问题不在于小部件在其空间包"内的行为,而在于空间的分配.

When I try your code, I see that the two widgets are both being allocated the same amount of space in the horizontal direction; the .frameLeft widget is then sticking to the left side of that space whereas the .frameMiddle widget is filling out the space it has been allotted. So the problem isn't the behavior of the widgets within their "space packet", but rather the allocation of space.

现在,使用pack,额外空间的分配由-expand 选项控制;额外的空间在启用了扩展的数据包之间共享.这意味着要获得您想要的布局,您应该简单地不要.frameLeft 设置 -expand 1.

Now, with pack, the allocation of the extra space is controlled by the -expand option; the extra room is shared between the packets that have expansion enabled for them. This means that to get the layout you want, you should simply not set -expand 1 for .frameLeft.

这篇关于Tcl/Tk:调整大小时帧行为异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆