Finding items by their position
The Canvas
class includes methods to retrieve item identifiers that are close to a canvas coordinate.
This is very useful because it saves us from storing each reference to a canvas item and then calculating their current position to detect which ones are within a specific area or closest to a certain point.
Getting ready
The following application creates a canvas with four rectangles, and changes the color of the one that is closest to the mouse pointer:

How to do it...
To find the closest item to the pointer, we pass the mouse event coordinates to the canvas.find_closest()
method, which retrieves the identifier of the item that is closest to the given position.
Once there is at least one item in the canvas, we can safely assume that this method will always return a valid item identifier:
import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() self.title("Finding canvas items") self.current = None self...