6

我有一个 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也不支持鼠标点击,但我不确定。

有办法吗?

4

2 回答 2

13

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

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函数 if self.shift_is_held

if event.button == 3:
    if self.shift_is_held:
        do_something()
    else:
        do_something_else()
于 2013-08-09T11:37:35.797 回答
2

如果您使用的是 Qt,那么您可以简单地使用:

def onClick(self, event)
    # queries the pressed modifier keys
    mods = QGuiApplication.queryKeyboardModifiers()
    if event.button == 1 and mods == Qt.ShiftModifier:
        # do something when shift-clicking
于 2017-07-05T09:19:46.647 回答