from Tkinter import * root = Tk() def callback(event): print("clicked at", event.x, event.y) canvas = Canvas(root, width=200, height=200) canvas.bind("", callback) canvas.pack() root.mainloop()
from tkinter import * root = Tk() def highlight(event): canvas.itemconfig(rect, fill='red') def unhighlight(event): canvas.itemconfig(rect, fill='blue') canvas = Canvas(root, width=200, height=200) rect = canvas.create_rectangle(50, 50, 150, 150, fill='blue') canvas.pack() canvas.tag_bind(rect, 'In this example, the rectangle on the canvas changes color to red when the user hovers the mouse over it and goes back to its original color (blue) when the mouse leaves the rectangle. The `tag_bind` method is used to bind events to the rectangle object. The package/library used for the examples is Tkinter, which is a standard Python library for GUI programming.', highlight) canvas.tag_bind(rect, ' ', unhighlight) root.mainloop()