'Determine which of two tkinter canvas items is above the other

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



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1