matplotlib 与事件处理程序的多个连接?

发布于 2024-11-27 09:13:47 字数 977 浏览 3 评论 0原文

import sys
import matplotlib
import matplotlib.pyplot as plt
print matplotlib.__version__, matplotlib.get_backend()

def hit(event):
  sys.stderr.write('hit\n')

fig = plt.figure()
cid0 = fig.canvas.mpl_connect('key_press_event', hit)
cid1 = fig.canvas.mpl_connect('button_press_event', hit)
print cid0, cid1
plt.show()

使用上面的代码,为什么我不能同时触发鼠标按下事件和按键事件?似乎在上面的顺序中只有按键事件起作用,而如果我交换第 10 行和第 11 行(顺序 cid0 和 cid1 分配),那么只有鼠标事件起作用。即,无论我第一个连接的哪个都会占用事件处理程序。这是 matplotlib 的内置限制,还是我试图以错误的方式连接多个事件?

编辑一些额外信息:我的 matplotlib.__version__1.1.0。我尝试使用 GTKAgg 和 TkAgg 后端获得相同的结果。使用 python 和 ipython,无论有或没有 -wthread -pylabipython qtconsole --pylab=inline,都没有什么区别。我得到的连接 ID 是 cid0 == cid1 == 6

编辑 2:我的问题今天仍然存在于 matplotlib 版本 1.2.xTkAgg 后端,sys .version 2.7.2+(默认,2011 年 10 月 4 日,20:06:09)[GCC 4.6.1]

import sys
import matplotlib
import matplotlib.pyplot as plt
print matplotlib.__version__, matplotlib.get_backend()

def hit(event):
  sys.stderr.write('hit\n')

fig = plt.figure()
cid0 = fig.canvas.mpl_connect('key_press_event', hit)
cid1 = fig.canvas.mpl_connect('button_press_event', hit)
print cid0, cid1
plt.show()

With the code above, why can't I have both mouse press event and key press events firing hit? It seems in the order above only the key press events work, whereas if I swap the lines 10 and 11 around (order cid0 and cid1 assignment), then only the mouse events work. I.e. whichever one I connected first hogs the event handler. Is this a built in limitation of matplotlib, or am I trying to connect multiple events in the wrong way?

edit with some extra info: My matplotlib.__version__ is 1.1.0. I have tried with GTKAgg and TkAgg backends with the same result. Using python and ipython, with or without -wthread -pylab, ipython qtconsole --pylab=inline, does not make a difference. The connection ids I get are cid0 == cid1 == 6.

edit 2: My problem still remains today with matplotlib version 1.2.x and TkAgg backend, sys.version 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-12-04 09:13:47

我认为您偶然发现了这个错误: 忽略多个 mpl_connect 调用

I think you stumbled upon this bug: Multiple mpl_connect calls ignored

傾旎 2024-12-04 09:13:47

我尝试了你的代码,两个动作(鼠标和键盘)都成功了:我每次都“击中”。

我使用 ubuntu 10.10、python 2.6.6 和 matplotlib 0.99.3,全部由 synaptic 安装(不是通过下载最新版本并运行 setup.py,因为这导致我之前遇到了几个大问题)。

我还安装了 python2.6-dev,因为这在大多数情况下添加了缺少的“.h”标头。

希望这有帮助。

I tried your code, and both the actions (mouse and keyboard) did the trick : i had "hit" every time.

I use ubuntu 10.10, python 2.6.6 and matplotlib 0.99.3, all installed by synaptic (not by downloading latest version and running setup.py, as this has lead me into several big problems previously).

I also have python2.6-dev installed, as this adds the missing ".h" headers in most cases.

Hope this helps.​

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文