user14745999
user14745999

Reputation:

How to add custom image to cursor when hovered over python tkinter window

I want to make a custom image cursor when the cursor is hovered over my tkinter window, something like this:

Black cursor

However, no matter how hard I google, I cannot find out how to add a custom cursor to my tkinter window, so I'm stuck with the default white cursor.

Default cursor

Is there anyway I can add a custom image to my cursor when it is hovered over my tkinter window?

Upvotes: 2

Views: 1530

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

I am assuming that you have a custom image with you already and it is a .cur file, if not, it should either be cur or ani or xbm files, which are the only supported cursor extensions. Once you have the file, you can specify it by using the cursor option of main window, like:

from tkinter import *

root = Tk()
path = '@Norm.ani' # Path to the image followed by @
root['cursor'] = path # Set the cursor 

Button(root,text='Anything').pack(padx=10,pady=10) # Demo button to show cursor

root.mainloop()

This applies the cursor to the entire window, though there are widgets to which custom cursors does not apply like Menu.

If you want custom cursor for just one widget, then use the cursor option of that widget, like:

Button(root,text='Anything',cursor=path).pack(padx=10,pady=10) # Applies JUST to button

Why do we use '@'?

From the docs:

"In place of a standard bitmap name, use the string '@' followed by the path name of the .xbm file.".

In your case the ani or cur file.

Upvotes: 4

Related Questions