I'm not incredibly familiar with the Canvas, but it looks like find_overlapping
is returning an ID for each oval that you're creating, starting at 1 and incrementing by 1. In fact, if you print the oval object:
print(my_oval)
it returns 1. And if you had a second and printed it, it would be 2.
So, it becomes just a problem of keeping track of the object IDs for each oval. Consider a dictionary to hold all of the IDs; and when you create an oval, you give it an id and insert it into the dictionary. Then, make a function (overlaps()
) that returns a list of all keys that match the overlapping values.
Take a look at this working example and let me know if anything doesn't make sense:
from tkinter import *
def overlaps((x1, y1, x2, y2)):
'''returns overlapping object ids in ovals dict'''
oval_list = [] # make a list to hold overlap objects
c_object = table.find_overlapping(x1, y1, x2, y2)
for k,v in ovals.items(): # iterate over ovals dict
if v in c_object: # if the value of a key is in the overlap tuple
oval_list.append(k)# add the key to the list
return oval_list
a = Tk()
# make a dictionary to hold object ids
ovals = {}
table = Canvas(a, width=500, height=300, bg='white')
table.pack()
# create oval_a and assign a name for it as a key and
# a reference to it as a value in the ovals dict.
# the key can be whatever you want to call it
# create the other ovals as well, adding each to the dict
# after it's made
oval_a = table.create_oval(40, 40, 80, 80)
ovals['oval_a'] = oval_a
oval_b = table.create_oval(60, 60, 120, 120)
ovals['oval_b'] = oval_b
oval_c = table.create_oval(120, 120, 140, 140)
ovals['oval_c'] = oval_c
# draw a rectangle
rectangle = table.create_rectangle(30, 30, 70, 70)
# print the return value of the overlaps function
# using the coords of the rectangle as a bounding box
print(overlaps(table.coords(rectangle)))
a.mainloop()