Search code examples
pythontkintercursor

Python tkinter how can I set the cursor as an image


I want the cursor to be an image when hovering over my canvas, when i run this it gives me 'bad cursor spec "pyimage1"' error. Is it possible?

from tkinter import*

root = Tk()
root.geometry('500x500+0+0')

custom_cursor = PhotoImage(file='custom_cursor.png')

canvas = Canvas(root, height=50, width=50, bg='black', cursor=custom_cursor)
canvas.pack()

root.mainloop()

Solution

  • No, it is not possible to use an image as a cursor. At least, not directly. The cursor option must be one of the following:

    • the name of a standard X11 cursor (see here for a list of valid cursor names)
    • the path to a file in the X11 or X10 cursor format
    • on Windows only, the path to a .ani or .cur file

    The definitive description of the cursor option is on the tcl/tk man page for Tk_GetCursor

    Since you are using a canvas, you can simulate a cursor with an image by drawing a normal image and updating it's position whenever the cursor moves, though it would lag very slightly being the real cursor.