为什么小部件的创建顺序很重要? [英] Why is the creation order of widgets important?

查看:34
本文介绍了为什么小部件的创建顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码工作正常.它显示一个 panedwindow,顶部有一个蓝色框,下面有一个绿色框:

The following code works fine. It displays a panedwindow, with a blue box on top and a green box below:

panedwindow .root -orient vertical -showhandle true -background red
frame .top -background blue -width 100 -height 100
frame .bot -background green -width 100 -height 100
.root add .top .bot
pack .root -expand true -fill both

但是,当我向下移动 panedwindow 命令时,事情就停止了.顶部的蓝色框未显示.相反,panedwindow 本身的红色会发光:

However, when I move the panedwindow command down, things stop working. The top, blue box is not shown. Instead, the red of the panedwindow itself shines through:

frame .top -background blue -width 100 -height 100
panedwindow .root -orient vertical -showhandle true -background red
frame .bot -background green -width 100 -height 100
.root add .top .bot
pack .root -expand true -fill both

为什么会这样?panedwindow 真的只能管理在它之后创建的小部件吗?我在打包器中看到了类似的行为,它会拒绝打包稍后出现的小部件 -in 小部件.

Why does this happen? Can a panedwindow really only manage widgets that were created after it? I've seen similar behavior with the packer, where it will refuse to pack widgets -in widgets that came later.

推荐答案

正如 Matt 所指出的,这完全与堆叠顺序有关.堆叠顺序只是应用程序中小部件的z 坐标"—你可以把它想象成除了屏幕上的自然 x 和 y 坐标之外,还有另一个垂直于屏幕平面的轴.所有的小部件都沿着这个轴位于某个地方.您在屏幕上实际看到的图像是沿该轴将所有小部件展平"在一起的结果.小部件在 x 和 y 平面中重叠的任何地方,堆叠顺序中较高的小部件就是您在该位置看到的.

It's all to do with stacking order, as Matt noted. Stacking order is just the "z-coordinate" for widgets in your app — you can think of it as if in addition to the natural x and y coordinates you have on the screen, there is another axis that is perpendicular to the plane of the screen. All the widgets lay somewhere along this axis. The image you actually see on screen is the result of "flattening" all the widgets together along that axis. Anywhere that widgets overlap in the x and y plane, the widget that is higher in the stacking order is what you see at that location.

小部件在 Tk 中的默认堆叠顺序由它们的创建顺序决定.它创建的小部件越早,它在堆叠顺序中的位置就越低 —想想屏幕本身在 z 坐标零处,随着越来越多的值从屏幕向您传来.创建的第一个小部件的堆叠顺序为零,第二个为一个,依此类推

The default stacking order of widgets in Tk is determined by their creation order. The earlier the widget it created, the lower it is in the stacking order — think of the screen itself being at z-coordinate zero, with increasing values coming towards you from the screen. The first widget created has stacking order zero, the second has one, etc.

解决您的问题最简单的方法就是按正确的顺序创建小部件,但是如果您对按照显示的顺序创建小部件一无所知,您可以稍后手动更改堆叠顺序,以确保小部件按您想要的顺序堆叠.例如,要将您的蓝框再次置于顶部,您可以添加:

The easiest solution to your problem is just to create the widgets in the right order, but if you are dead set on creating the widgets in the order you've shown, you can manually change the stacking order later, to ensure the widgets are stacked in the order you want. For example, to bring your blue frame to the top again, you could add:

raise .top .root

这告诉 Tk 更改 .top 的堆叠顺序,使其位于 .root 的上方".

This tells Tk to change the stacking order of .top so that it is "above" .root.

当我使用窗格窗口小部件时,我倾向于让它管理子小部件—对我来说,它在概念上只是一个具有额外行为的框架,并且由于我使用框架将相关的小部件组合在一起,因此我以相同的方式使用窗格窗口.此策略还巧妙地回避了堆叠顺序的问题,因为它要求您首先创建窗格窗口 —您必须这样做,因为在创建小部件本身之前,您无法创建小部件的子部件.因此,我会像这样修改您的示例:

When I use the paned window widget, I tend to have it manage child widgets — to me, it is conceptually just a frame with extra behaviors, and since I use frames to group related widgets together, I use the paned window the same way. This policy also neatly sidesteps the question of stacking order since it requires that you have created the paned window first — you must, because you can't create children of a widget until the widget itself is created. Thus I would modify your example like this:

panedwindow .root -orient vertical -showhandle true -background red
frame .root.top -background blue -width 100 -height 100
frame .root.bot -background green -width 100 -height 100
.root add .root.top .root.bot

对我来说,这使得 .root.root.top.root.bot 之间的关系变得清晰:两个框架是内部"窗格窗口.自然的堆叠顺序恰好是正确的,每个人都很高兴.

To me, this makes the relationship between .root and .root.top and .root.bot clear: the two frame are "inside" the paned window. The natural stacking order happens to be the correct one, and everybody is happy.

这篇关于为什么小部件的创建顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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