Question

What does the 'find_overlapping' function return in python tkinter Canvas?

from tkinter import *
a = Tk()
table = Canvas(a, width=500, height=300, bg='white')
table.pack()

my_oval = table.create_oval(40, 40, 80, 80)

c_object = table.find_overlapping(30, 30, 70, 70)
print(c_object)

a.mainloop()
>>> (1, )

A tuple is returned to the console. What I want is to assign an ID to my_oval and instead of a tuple returned, I would like the ID returned.

my_oval.[some_kind_of_id] = 'myID'
c_object = table.find_overlapping(30, 30, 70, 70)
print(c_object)
>>> (1, )
# I want 'myID' to be printed

*Note: The above code will return a syntax error because of my [some_kind_of_id] assignment. The 3 points I want:

1) assign some kind of ID to a shape

2) find_overlapping of a certain point

3) if the shape overlaps the point, return the shape's ID

Edit: To make clear what I want, I'm making a game in tkinter and if my square is touching a wall, it stops, but if it touches a coin, it keeps going. So if the id returned by the square (by built in function 'find_overlapping') belongs to a wall, the square will stop.

Was it helpful?

Solution

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top