确定两个tkinter画布项中的哪一个高于另一个



有没有一种方法可以确定哪个项目在tkinter画布的显示顺序上处于最顶端,给定它们各自的id?

您可以使用canvas.find_all()获取所有项目ID,并根据文档:"项目按堆叠顺序返回

下面是一个查找检查列表中最上面项目的示例:

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)

最新更新