确定两个 tkinter canvas 项目中的哪一个在另一个之上
Determine which of two tkinter canvas items is above the other
有没有一种方法可以根据 tkinter
canvas 各自的 ID 来确定哪个项目在显示顺序中排在最前面?
您可以使用 canvas.find_all()
获取所有项目 ID,并根据 document:“项目按堆叠顺序返回,最低的项目在前”。 =18=].
下面是一个在检查列表中查找最顶层项目的示例:
check_ids = [1, 3, 5, 7]
all_ids = list(canvas.find_all())
# remove item IDs in all_ids that are not in check_ids
for x in all_ids[:]:
if x not in check_ids:
all_ids.remove(x)
# now all_ids contains only ID from check_ids sorted by stacking order
# so the last item ID is the topmost item in the list
topmost = all_ids[-1]
print(topmost)
有没有一种方法可以根据 tkinter
canvas 各自的 ID 来确定哪个项目在显示顺序中排在最前面?
您可以使用 canvas.find_all()
获取所有项目 ID,并根据 document:“项目按堆叠顺序返回,最低的项目在前”。 =18=].
下面是一个在检查列表中查找最顶层项目的示例:
check_ids = [1, 3, 5, 7]
all_ids = list(canvas.find_all())
# remove item IDs in all_ids that are not in check_ids
for x in all_ids[:]:
if x not in check_ids:
all_ids.remove(x)
# now all_ids contains only ID from check_ids sorted by stacking order
# so the last item ID is the topmost item in the list
topmost = all_ids[-1]
print(topmost)