Felipe Martins
Felipe Martins

Reputation: 220

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?

Upvotes: 0

Views: 124

Answers (1)

acw1668
acw1668

Reputation: 46751

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)

Upvotes: 2

Related Questions