Tkinter Canvas move item to top level

被刻印的时光 ゝ 提交于 2019-12-08 19:43:54

问题


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.


回答1:


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

canvas.tag_raise(firstRect)

Or:

canvas.tag_lower(secondRect)



回答2:


If you have multiple items on the canvas and you don't know which one its going to overlap, then do this.

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


来源:https://stackoverflow.com/questions/10959858/tkinter-canvas-move-item-to-top-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!