如何让 Matplotlib 事件相互通信?
我正在使用 Matplotlib 构建交互式绘图工具,但在并发事件方面遇到问题。具体来说,我想同时使用键盘和鼠标,但我不确定如何在事件处理程序之间传递信息。如何让鼠标事件处理程序知道当前正在按下某个键?我正在尝试使用全局变量,如下所示,但它不起作用。
任何帮助表示赞赏。
import matplotlib.pyplot as plt
SHIFT_PRESSED = False
def on_key_press(event):
if event.key == 'shift':
SHIFT_PRESSED = True
def on_key_release(event):
if event.key == 'shift':
SHIFT_PRESSED = False
def on_click(event):
print(SHIFT_PRESSED) # never prints True
fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('key_press_event', on_key_press)
fig.canvas.mpl_connect('key_release_event', on_key_release)
plt.show()
I'm using Matplotlib to build an interactive drawing tool but I am having trouble with concurrent events. Specifically I'd like to use the keyboard and mouse together, but I'm not sure how to pass information between event handlers. How can I get the mouse event handler to know that a key is currently being pressed? I am trying to use a global variable, as below, but it's not working.
Any help appreciated.
import matplotlib.pyplot as plt
SHIFT_PRESSED = False
def on_key_press(event):
if event.key == 'shift':
SHIFT_PRESSED = True
def on_key_release(event):
if event.key == 'shift':
SHIFT_PRESSED = False
def on_click(event):
print(SHIFT_PRESSED) # never prints True
fig, ax = plt.subplots()
fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('key_press_event', on_key_press)
fig.canvas.mpl_connect('key_release_event', on_key_release)
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论