matplotlib:如何拿起移位点击图



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

有办法吗?

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

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

然后,如果是self.shift_is_held,则可以检查您的onClick函数。

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

如果你在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

相关内容

  • 没有找到相关文章

最新更新