Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Canvas move item to top level

I have a Tkinter Canvas widget (Python 2.7, not 3), and on this Canvas I have different items. If I create a new item that overlaps an old item, It will be in front. How can I now move the old item in front of the newly created one, or even in front of all other items on the Canvas?

Example code so far:

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

now I want firstRect to be in front of secondRect.

like image 393
Peter Kramer Avatar asked Jun 09 '12 09:06

Peter Kramer


1 Answers

Use the tag_lower() and tag_raise() methods for the Canvas object:

canvas.tag_raise(firstRect)

Or:

canvas.tag_lower(secondRect)
like image 52
Joel Cornett Avatar answered Oct 21 '22 02:10

Joel Cornett