Tkinter中的matplotlib画布上的光标 [英] Cursor over matplotlib canvas in Tkinter

查看:114
本文介绍了Tkinter中的matplotlib画布上的光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tkinter模块在Python上编写GUI包,并通过 FigureCanvasTkAgg 使用matplotlib显示一些图形数据.我还想通过 canvas.mpl_connect 方法与这种直观的数据表示形式进行交互.一切正常,但我对默认的类似箭头的光标不满意:它不允许精确点击我的数据(实际的点击点"与箭头的尖端不匹配)并且它对用户隐藏了一些点击品脱附近的区域.所以问题是如何更改默认光标?我尝试通过Tk config方法(例如 master.config(cursor ="tcross"))进行更改,但是显然matplotlib canvas具有其自己的光标配置,因为该字符串仅会更改所有部分上方的光标外观除MPL画布外,主窗口的顶部,仍在画布上方显示默认箭头.

解决方案

首先,需要注意以下几点:我将深入探讨一些实现细节.我认为工具栏是最近重构的,因此其中的一些可能会随着下一个matplotlib版本的改变而改变.此外,我为游标指定的值仅适用于 TkAgg 后端.其他后端的基本概念相似,但是 cursord 的位置和特定于后端的游标将有所不同.作为 Qt4Agg 的示例, cursord 字典位于 backend_qt4 中,而不是 backend_qt4agg 中,并且值是Qt游标:例如QtCore.Qt.ArrowCursor.

正如我在评论中提到的,请查看 matplotlib的 Cursor 小部件.但是,如何自定义 matplotlib 使用的光标的更广泛问题有点棘手.

您不能直接更改 matplotlib 使用的光标(例如 master.config(cursor='whatever'))的原因是默认工具栏覆盖了它(不同工具的不同光标在工具栏).

一个选项是不添加工具栏.在这种情况下,您只需调用 parent.config(cursor='tcross') 即可完成.但是,您大概希望该工具栏出现以便更轻松地进行平移/缩放/等操作.

工具栏为每个工具使用当前光标的字典:backend_tkagg.cursord.关键是 int 代码,因此通过 matplotlib.backend_bases.cursors 引用它们更具可读性.默认工具是 cursors.POINTER .

举个简单的例子(为简单起见,我使用 pyplot 界面来构建画布、图形等)

  import matplotlibmatplotlib.use('TkAgg')导入matplotlib.pyplot作为plt从matplotlib.backend_bases导入游标导入 matplotlib.backends.backend_tkagg 作为 tkagg# 将默认光标更改为任何有效的 TK 光标# 要隐藏它,您可以使用字符串none"(或者在 Windows 上可能使用no")tkagg.cursord [cursors.POINTER] ='coffee_mug'无花果,ax = plt.subplots()plt.show()

请注意,您还可以以相同的方式控制任何工具(例如缩放、平移等)的光标.您的选项为 cursor.HAND cursor.POINTER cursor.SELECT_REGION cursor.MOVE .

I'm writing a GUI package on Python with Tkinter module and using matplotlib via FigureCanvasTkAgg to show some graphical data. I also want to interact with this visual representation of the data via canvas.mpl_connect method. Everything works just fine, but I do not satisfied with default arrow-like cursor: it does not allow precise clicking on my data (the actual "click point" does not match with the tip of the arrow) and also it hides from user some area near the click pint. So the question is how to change the default cursor? I tried change it through Tk config methods (like master.config(cursor="tcross")), but apparently matplotlib canvas has its own cursor configuration because this string only changes appearance of the cursor above all parts of main window except MPL canvas, above canvas the default arrow is still being shown.

解决方案

First off, a couple of caveats: I'm about to dive into some implementation details. I think the toolbar was recently refactored, so some of this could change with the next version of matplotlib. Furthermore, this values I've specified for the cursor only apply to the TkAgg backend. The general concept is similar for other backends, but the location of cursord and the backend-specific cursors will vary. As an example for Qt4Agg, the cursord dict is in backend_qt4 instead of backend_qt4agg, and the values are Qt cursors: e.g. QtCore.Qt.ArrowCursor.

As I mentioned in my comment, have a look at matplotlib's Cursor widget. However, the broader issue of how to customize the cursor(s) matplotlib uses is a bit hairier.

The reason that you can't directly change the cursor matplotlib uses (e.g. master.config(cursor='whatever')) is that the default toolbar overrides it (different cursors for different tools on the toolbar).

One option is to not add the toolbar. In that case, you can just call parent.config(cursor='tcross') and be done with it. However, you presumably want the toolbar to be present for easier panning/zooming/etc.

The toolbar uses a dict of present cursors for each tool: backend_tkagg.cursord. The keys for this are int codes, so it's a bit more readable to refer to them through matplotlib.backend_bases.cursors. The default tool is the cursors.POINTER.

As a quick example (For simplicity, I'm using the pyplot interface to build the canvas, figure, etc.)

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

from matplotlib.backend_bases import cursors
import matplotlib.backends.backend_tkagg as tkagg

# Change the default cursor to any valid TK cursor
# To hide it, you'd use the string "none" (or possibly "no" on windows)
tkagg.cursord[cursors.POINTER] = 'coffee_mug' 

fig, ax = plt.subplots()
plt.show()

Note that you can also control the cursor for any tool (e.g. zoom, pan, etc) in the same way. Your options are cursor.HAND, cursor.POINTER, cursor.SELECT_REGION, and cursor.MOVE.

这篇关于Tkinter中的matplotlib画布上的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆