python - 是否可以使用 Python Tkinter 制作自定义鼠标光标? (将 matplotlib 与 TkAgg 后端一起使用)

标签 python matplotlib tkinter mouse-cursor

这可能只是一个一般的 Python Tkinter 问题,不一定是 matplotlib 问题。

因此,我正在使用 Matplotlib“TkAgg”后端(使用 TkInter 将 Agg 渲染到 Tk Canvas )在 matplotlib 之上开发一套相当大的绘图功能。我正在使用 matplotlib 提供的一些开箱即用的默认缩放功能……特别是默认 matplotlib 工具栏上的“缩放到框”按钮。我通过子类化现有的“matplotlib.backends.backend_tkagg.NavigationToolbar2TkAgg”类来创建自己的工具栏。

差不多,这里的问题是我讨厌“缩放到框”使用的默认图标(Tkinter“tcross”)。我已经弄清楚如何使用不同的 Tkinter 内置光标(例如,这会将光标更改为“plus”而不是“tcross”):

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.backend_bases
import matplotlib.backends.backend_tk_agg

matplotlib.backends.backend_tkagg.cursord[matplotlib.backend_bases.cursors.SELECT_REGION] = "plus"

一般来说,我知道要将当前鼠标光标更改为工具栏类中内置的 Tkinter 光标之一,我可以调用:

self.window.configure(cursor="cursor_name")

所以我真正非常喜欢的是能够在用户处于“缩放模式”时使用放大镜图标。我已经有了我想使用的放大镜图标的 .ppm 和所有内容,但我终生无法弄清楚如何将放大镜用作鼠标光标图标。是否可以在 Python Tkinter 中使用自定义图像作为鼠标光标?帮助!

平台注意事项:这需要在 Mac OS X 10.5+、RedHat Enterprise Linux 5 和可能的 Solaris 10 上运行,因此不希望使用特定于平台的解决方案。

最佳答案

像这样的东西适用于 unix X11 XBM 文件:

import Tkinter
t = Tkinter.Tk()
t.configure(cursor=('@/usr/include/X11/bitmaps/star', '/usr/include/X11/bitmaps/starMask', 'black', 'white'))
t.mainloop()

至于 Mac,来自“Tk_GetCursorFromData”的手册页:

The Macintosh version of Tk supports all of the X cursors and will also accept any of the standard Mac cursors including ibeam, crosshair, watch, plus, and arrow. In addition, Tk will load Macintosh cursor resources of the types crsr (color) and CURS (black and white) by the name of the of the resource.
The application and all its open dynamic library's resource files will be searched for the named cursor. If there are conflicts color cursors will always be loaded in preference to black and white cursors.

关于python - 是否可以使用 Python Tkinter 制作自定义鼠标光标? (将 matplotlib 与 TkAgg 后端一起使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1877360/

相关文章:

python - 使用 Python mlxtend 的关联规则

python - Matplotlib:如何让条形图填满整个 x 轴?

python 在 matplotlib.image.AxesImage 对象上绘制峰值

python - Tkinter 使用 delete 从 Canvas 中删除对象

python-3.x - Tkinter 单选按钮 : Remove dot along with image

python - args 在 python 中被映射到 kwargs

python - 在无法查看电子邮件地址的情况下验证电子邮件地址

Python - 如何将 ttkthemes 包中的主题添加到 guizero 应用程序?

python - 如何通过 python 调用/运行软件?

python - Python 中具有无限小点大小的散点图