tkinter: How to change cursor over canvas items? tkinter: How to change cursor over canvas items? tkinter tkinter

tkinter: How to change cursor over canvas items?


The only way to change the cursor is via changing how it presents on the canvas. By checking every time the mouse moves whether it is inside the boundary box of the item you want it to change over you can achieve this effect.

from tkinter import *canvas = Canvas(width=200,height=200)canvas.pack()rec = canvas.create_rectangle(100,0,200,200,fill="red")#example objectdef check_hand(e):#runs on mouse motion    bbox= canvas.bbox(rec)    if bbox[0] < e.x and bbox[2] > e.x and bbox[1] < e.y and bbox[3] > e.y:#checks whether the mouse is inside the boundrys        canvas.config(cursor="hand1")    else:        canvas.config(cursor="")canvas.bind("<Motion>",check_hand)#binding to motion


Spent some time figuring this out.

Below method works for all shapes using tag_bind() method with Enter and Leave.

import tkinter as tkmain_window = tk.Tk()def check_hand_enter():    canvas.config(cursor="hand1")def check_hand_leave():    canvas.config(cursor="")canvas = tk.Canvas(width=200, height=200)tag_name = "polygon"canvas.create_polygon((25, 25), (25, 100), (125, 100), (125, 25), outline='black', fill="", tag=tag_name)canvas.tag_bind(tag_name, "<Enter>", lambda event: check_hand_enter())canvas.tag_bind(tag_name, "<Leave>", lambda event: check_hand_leave())canvas.pack()main_window.mainloop()