Mathieu Robert
Mathieu Robert

Reputation: 334

Python : Only one element return in tuple return by find_closest

I've this code who create a rectangle an say if there is something else near of him :

def creaRect(event):
    #rect = Rectangle.Rectangle(canvas, event, CanWidth=Width, CanHeight=Height)
    width, height = 25, 25
    x, y = event.x, event.y
    x1 = int(x-width/2)
    y1 = int(y-height/2)
    x2 = int(x+width/2)
    y2 = int(y+height/2)
    rect = canvas.create_rectangle((x1, y1, x2, y2), outline="red", width=1, tags="bloc")
    Rectangle.OidRect.append(rect)

    near = canvas.find_closest(x, y, 200)
    print(len(near))

But the lenght of the tuple return by find_closest is every time 1, while i create many rectangles on the same position or really near. For me find_closest should return a tuple with the id of all the items around x, y coordinates in a range of 200. Is it something i don't understand or do wrong ?

Upvotes: 2

Views: 1819

Answers (1)

Mike
Mike

Reputation: 49463

From the documentation about find_closest() here

find_closest(self, x, y, halo=None, start=None)

Return item which is closest to pixel at X, Y. If several match take the top-most. All items closer than HALO are considered overlapping (all are closests). If START is specified the next below this tag is taken.

So find_closest() will only give you the one closest item. If you want to find multiple items within a distance from a point (which is what is sounds like you were going for) try:

find_overlapping(x1, y1, x2, y2)
Finds all items that overlap the given rectangle, or that are completely enclosed by it.
x1 - Left edge.
y1 - Upper edge.
x2 - Right edge.
y2 - Lower edge.
Returns:
A tuple containing all matching items.

Upvotes: 6

Related Questions