Python Tkinter画布将项目移动到顶层

Python Tkinter画布将项目移动到顶层,python,tkinter,widget,tkinter-canvas,Python,Tkinter,Widget,Tkinter Canvas,我有一个Tkinter画布小部件(Python2.7,而不是3),在这个画布上我有不同的项目。如果我创建一个与旧项目重叠的新项目,它将位于前面。现在如何将旧项目移动到新创建的项目之前,或者甚至移动到画布上所有其他项目之前 到目前为止的示例代码: from Tkinter import * root = Tk() canvas = Canvas(root,width=200,height=200,bg="white") canvas.grid() firstRect = canvas.create

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

到目前为止的示例代码:

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前面。

画布
对象使用
tag\u lower()
tag\u raise()
方法:

canvas.tag_raise(firstRect)
或:


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

# 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)

这是把它提高了一级还是到了前面?@PeterKramer:我想是到了前面。这是一种方法,你只需将每个重叠的项目提高一个级别,最终将完全取消效果。要使其工作,您需要将您想要的对象的标记放在“object”之前,所以这样做:
canvas.tag\u-raise(这个应该是最上面的,object)
。更合适的方法是
canvas.tag\u-raise(这个应该是最上面的,'all')
# 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)