将鼠标悬停在 python tkinter window 上时如何将自定义图像添加到光标

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

当光标悬停在我的 tkinter window 上时,我想制作一个自定义图像光标,像这样:

然而,无论我多么努力 google,我都找不到如何将自定义光标添加到我的 tkinter window,所以我只能使用默认的白色光标。

当鼠标悬停在我的 tkinter 上时,我是否可以向光标添加自定义图像 window?

我假设您已经有了一个自定义图像,它是一个 .cur 文件,如果没有,它应该是 curanixbm 文件,这是唯一受支持的游标扩展。获得文件后,您可以使用 main window 的 cursor 选项指定它,例如:

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()

这会将光标应用于整个 window,尽管有些小部件不应用自定义光标,例如 Menu

如果您只想为一个小部件自定义光标,请使用该小部件的光标选项,例如:

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

为什么要用'@'?

来自文档:

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

在你的情况下 anicur 文件。