簡體   English   中英

Tkinter Canvas將項目移至頂級

[英]Tkinter Canvas move item to top level

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

目前為止的示例代碼:

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

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

canvas.tag_raise(firstRect)

要么:

canvas.tag_lower(secondRect)

如果畫布上有多個項目,並且您不知道它將重疊哪個項目,那么請執行此操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM