Home > Back-end >  Determine which of two tkinter canvas items is above the other
Determine which of two tkinter canvas items is above the other

Time:03-12

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

CodePudding user response:

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)
  • Related