Matplotlib: How to choose a replacement digit?

I have matplotlib and I created button_press_event as follows:

 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 

Now you can change the wheel click handler to something like this

  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 does not seem to support keys, and key_press_event does not support mouse clicks, but I'm not sure.

Is there any way?

+6
source share
2 answers

You can also associate keypress and keypress events and do something like:

 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 

Then you can check your onClick function if self.shift_is_held .

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

If you are on Qt, you can simply use:

 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 
+1
source

Source: https://habr.com/ru/post/951370/


All Articles