Tkinter 中的堆叠顺序维护 [英] Stacking Order Maintenance in Tkinter

查看:39
本文介绍了Tkinter 中的堆叠顺序维护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Tkinter 中保持小部件(尤其是图像)的堆叠顺序一致?例如,我可能在画布上的同一位置有两个矩形、两个三角形和一个圆.圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制在矩形的顶部和三角形的后面.默认行为当然是将最后绘制的对象放在顶部的给定位置.

Is there a way to keep consistent stacking order of widgets (particularly images) from within Tkinter? For example, I may have two rectangles, two triangles, and a circle all at the same location on the canvas. The circle moves to wherever the mouse was last clicked, but I always want it to be drawn on top of rectangles and behind triangles. The default behavior is of course to place the last-drawn object at a given location on top.

到目前为止,我只找到了 Lift() 和 lower() 方法来解决 Tkinter 中的堆叠问题.理论上,我可以在每次重新绘制时进行一些凌乱的检查,以查看目标处是否存在三角形,并对那里的每个形状进行一些提升()/降低(),直到事情达到我想要的方式为止,但这似乎是一个荒谬的解决方案.我希望在画布对象上有一种层"变量,所以它会被动地记住第 3 层的项目应该总是在第 1 层的后面.

I've only found the lift() and lower() methods to address stacking in Tkinter thus far. I could theoretically make some messy checks at every re-draw to see whether triangles exist at the destination and do some lift()/lower() for each shape there until things are the way I want, but it seems an absurd solution. I expected there to be a sort of "layer" variable on canvas objects so it would passively remember that items of layer 3 should always be behind those of layer 1.

如果有一种简单的方法可以解决我的问题,我深表歉意,但到目前为止,我的所有搜索都无济于事.我将不胜感激!

I apologize if there is a simple method to solve my problem, but as of yet all my searching has been to no avail. I will be thankful for any input!

推荐答案

用triangle"标记所有三角形,用rectangle"标记所有矩形,用circle"标记所有圆形.然后,您可以使用 liftlower 方法一次升高或降低所有三角形、圆形或矩形.它只需要几个语句,即使您有数千个项目,它也会在眨眼之间发生.

Tag all triangles with "triangle", all rectangles with "rectangle" and all circles with "circle". You can then use the lift or lower method to raise or lower all triangles, circles or rectangles at once. It takes just a couple of statements and will happen in less than the blink of an eye even if you have thousands of items.

既然你问到内置层机制:tk 没有这样的东西,但它真的很容易添加,所有工具都在那里.因此,与其在各处散布 liftlower 命令,不如让您的所有绘图通过一个特殊的命令来处理您的分层.

Since you asked about a built-in layer mechanism: tk has no such thing but it's really easy to add, all the tools are there. So, instead of sprinkling lift or lower commands everywhere, have all your drawing go through a special command that handles the layering for you.

例如,下面是一个接受图层编号并确保它绘制的所有内容都在该图层上的命令示例:

For example, here is a an example of a command that accepts a layer number and makes sure everything it draws is on that layer:

def add_to_layer(self, layer, command, coords, **kwargs):
    layer_tag = "layer %s" % layer
    if layer_tag not in self._layers: self._layers.append(layer_tag)
    tags = kwargs.setdefault("tags", [])
    tags.append(layer_tag)
    item_id = command(coords, **kwargs)
    self._adjust_layers()
    return item_id

def _adjust_layers(self):
    for layer in sorted(self._layers):
        self.canvas.lift(layer)

然后你会这样称呼它:

# draw a circle on layer 1:
self.add_to_layer(1, self.canvas.create_oval, (x0,y0,x1,y1), outline="red")
# draw a square on layer 2:
self.add_to_layer(2, self.canvas.create_rectangle, (x0,y0,x1,y1), fill="blue")

有很多其他方法可以完成同样的事情,以上只是我头脑中的一个快速而肮脏的例子(并不能保证 100% 正确,但我认为它说明了这一点).

There are many other ways to accomplish the same thing, the above is just a quick and dirty example off the top of my head (and not guaranteed to be 100% correct, but I think it illustrates the point).

基本思想是所有对象的创建都通过一个函数,该函数可以在每次创建项目时调整堆叠顺序.这样你只需在一个地方添加那些提升/降低命令.

The basic idea is that all object creation goes through a single function which can adjust the stacking order each time an item is created. That way you only have to add those lift/lower commands in one place.

这篇关于Tkinter 中的堆叠顺序维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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