Tkinter Canvas将项目移至顶级

时间:2012-06-09 09:27:56

标签: python tkinter widget tkinter-canvas

我有一个Tkinter Canvas小部件(Python 2.7,而不是3),在这个Canvas上我有不同的项目。如果我创建一个与旧项目重叠的新项目,它将在前面。我现在如何将旧项目移动到新创建的项目前面,甚至在画布上的所有其他项目前面?

到目前为止的示例代码:

from Tkinter import *
root = Tk()
canvas = Canvas(root,width=200,height=200,bg="white")
canvas.grid()
firstRect = canvas.create_rectangle(0,0,10,10,fill="red")
secondRect = canvas.create_rectangle(5,5,15,15,fill="blue")

现在我希望firstRect位于secondRect前面。

2 个答案:

答案 0 :(得分:10)

tag_lower()对象使用tag_raise()Canvas方法:

canvas.tag_raise(firstRect)

或者:

canvas.tag_lower(secondRect)

答案 1 :(得分:0)

如果画布上有多个项目而您不知道哪个项目会重叠,请执行此操作。

# find the objects that overlap with the newly created one
# x1, y1, x2, y2 are the coordinates of the rectangle

overlappers = canvas.find_overlapping(x1, y1, x2, y2)

for object in overlappers:
    canvas.tag_raise(object)