2012-06-09 36 views
5

Tengo un widget Tkinter Canvas (Python 2.7, no 3), y en este lienzo tengo diferentes elementos. Si creo un nuevo elemento que se superpone a un elemento anterior, estará al frente. ¿Cómo puedo mover el objeto anterior frente al recién creado, o incluso delante de todos los demás elementos del lienzo?Lienzo de Tkinter mover elemento al nivel superior

Código de ejemplo hasta el momento:

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

ahora quiero firstRect a estar frente a secondRect.

Respuesta

8

Uso los tag_lower() y tag_raise() métodos para el objeto Canvas:

canvas.tag_raise(firstRect) 

O:

canvas.tag_lower(secondRect) 
+0

¿Eso lo eleva por un nivel o al frente? –

+3

@ PeterKramer: al frente, creo. Aquí está el [enlace] (http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.tag_lower-method) –

0

Si tiene varios elementos en el lienzo y no saber cuál va a su superposición, luego haz esto.

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