问题
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]
回答1:
I think you stumbled upon this bug: Multiple mpl_connect calls ignored
回答2:
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.
来源:https://stackoverflow.com/questions/6894366/matplotlib-multiple-connections-to-an-event-handler