【发布时间】:2011-10-17 04:27:54
【问题描述】:
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 分配),那么只有鼠标事件起作用。 IE。无论我先连接哪个,都会占用事件处理程序。这是 matplotlib 的内置限制,还是我试图以错误的方式连接多个事件?
编辑一些额外的信息:我的matplotlib.__version__ 是1.1.0。我曾尝试使用GTKAgg 和TkAgg 后端,结果相同。使用 python 和 ipython,不管有没有 -wthread -pylab、ipython qtconsole --pylab=inline,都没有区别。我得到的连接 ID 是cid0 == cid1 == 6。
edit 2:我的问题仍然存在,matplotlib 版本 1.2.x 和 TkAgg 后端,sys.version 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1]
【问题讨论】:
-
您的代码对我来说很好用。我已经尝试过使用“ipython -pylab”,然后是“run yourcode”和“python yourcode.py”——干杯。我想我正在使用 GTK 后端,但我不确定。你在什么条件下跑步?
-
有趣。你能告诉我你的
sys.version、matplotlib.backends.backend和你的matplotlib.__version__吗? -
对于我来说也很好用(都打印
hit),使用2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3],Matplotlib 版本0.99.1.1和TkAgg作为默认后端。 -
我错了,肯定不是 GTK 后端。 sys.version => " 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \n [GCC 4.4.5]" matplotlib.backends.backend => TkAgg matplotlib.__version__ => '0.99.3 '
-
我注意到
cid0 == cid1,闻起来很有趣。如果我使用hit_ = lambda x: hit(x),然后将其中一个连接到hit_,我会得到cid0 != cid1,并且事件处理程序会按预期为两个用例触发。这是我将使用的当前解决方法,但如果有人能找到问题的根本原因,我很想听听,谢谢!
标签: python events event-handling matplotlib