如何设置Python tkinter canvas元素的z索引? [英] How do I set the z index of an Python tkinter canvas element?
问题描述
如何在初次启动画布时设置tkinter画布元素(圆形,矩形)的z-index?或者我可以在绘制例如时直接设置z-index一个圆圈?不幸的是,我对 canvas.tag_lower("tag_name")
所做的工作不多.我以为这个命令会将标记的所有元素都移回一级-是这样吗?
how can I set the z-index of tkinter canvas elements (circle, rectangle) on initial startup of the canvas? Or can I set a z-index directly when drawing e.g. a circle? Unfortunately I could not do much with canvas.tag_lower("tag_name")
. I thought this command moves all elements of a tag one level back - is that so?
class CanvasGui(tk.Canvas):
def __init__(self, master):
super().__init__(master, bg="#FFF", highlightthickness=0, borderwidth=0)
self.pack(fill="both", expand=True)
self.setCanvasLayers()
def setCanvasLayers(self):
self.tag_lower("highlightGridPoint")
self.tag_lower("grid")
我认为,标记为"highlightGridPoint"的元素现在应该位于最远的背景中,标记为"grid"的元素应该位于最远的背景中.但是,如果我运行该程序,事实并非如此……
In my opinion, the elements with the tag "highlightGridPoint" should now be in the farthest background and the elements with the tag "grid" in the foreground. But if I run the program, it's not so...
推荐答案
您必须先创建标签,然后才能设置标签的顺序.您的代码会升高和降低尚不存在的标签.
You can't set the ordering of a tag until after the tag has been created. Your code raises and lowers tags that don't yet exist.
您可以通过注意到以下代码在行为上的差异来看到这一点:
You can see this by noticing the difference in behavior between this code:
canvas.setCanvasLayers()
canvas.create_rectangle(10,10,50,50, fill="red", tags=("red",))
canvas.create_rectangle(20,20,60,60, fill="green", tags=("green",))
...以及此代码:
canvas.create_rectangle(10,10,50,50, fill="red", tags=("red",))
canvas.create_rectangle(20,20,60,60, fill="green", tags=("green",))
canvas.setCanvasLayers()
这篇关于如何设置Python tkinter canvas元素的z索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!