matplotlib:如何拿起班次点击数字?

时间:2013-08-09 06:19:45

标签: python matplotlib mouseevent shift modifier-key

我有一个matplotlib,我创建了一个button_press_event,如下所示:

self.fig.canvas.mpl_connect('button_press_event', self.onClick)

def onClick(self, event)
    if event.button == 1:
        # draw some artists on left click

    elif event.button == 2:
        # draw a vertical line on the mouse x location on wheel click

    elif event.button == 3:
        # clear artists on right click

现在可以将滚轮点击处理程序修改为类似这样的

    elif event.button == 2 or (event.button == 1 and event.key == "shift"):
        # draw a vertical line on the mouse x location 
        # on wheel click or on shift+left click 
        # (alternative way if there is no wheel for example)

似乎button_press_event不支持密钥,key_press_event不支持鼠标按钮点击,但我不确定。

有办法吗?

2 个答案:

答案 0 :(得分:10)

您还可以绑定按键和按键释放事件,并执行以下操作:

self.fig.canvas.mpl_connect('key_press_event', self.on_key_press)
self.fig.canvas.mpl_connect('key_release_event', self.on_key_release)

...

def on_key_press(self, event):
   if event.key == 'shift':
       self.shift_is_held = True

def on_key_release(self, event):
   if event.key == 'shift':
       self.shift_is_held = False

然后,您可以检查onClick函数self.shift_is_held

if event.button == 3:
    if self.shift_is_held:
        do_something()
    else:
        do_something_else()

答案 1 :(得分:0)

如果您使用的是Qt,那么您只需使用:

startOfYear