2012-06-09 55 views
5

我有一個Tkinter帆布部件(Python 2.7,而不是3),並在這個畫布上我有不同的項目。如果我創建了一個與舊項目重疊的新項目,它將在前面。我現在如何將舊項目移動到新創建的項目之前,甚至是在Canvas上的所有其他項目之前?Tkinter帆布移動項目到頂級

示例代碼至今:

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

回答

8

使用tag_lower()tag_raise()方法爲Canvas對象:

canvas.tag_raise(firstRect) 

或者:

canvas.tag_lower(secondRect) 
+0

是否會將它提升一級或前? –

+3

@PeterKramer:我相信前面。這是[鏈接](http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.tag_lower-method) –

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)