确定两个 tkinter 画布项目中哪一个位于另一个之上

发布于 2025-01-13 06:46:43 字数 51 浏览 0 评论 0原文

有没有办法根据各自的 id 确定哪个项目在 tkinter 画布的显示顺序上位于最上面?

Is there a way to determine which item is topmost on the displaying order of a tkinter canvas given their respective id's?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2025-01-20 06:46:43

您可以使用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)

You can use canvas.find_all() to get all the item IDs and according to the document: "The items are returned in stacking order, with the lowest item first".

Below is an example to find the topmost item in a check list:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文