2013-08-09 7 views
6

मैं एक matplotlib है और मैं एक button_press_event इस तरह बनाया है:matplotlib: आकृति पर शिफ्ट क्लिक कैसे करें?

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 माउस बटन क्लिक का समर्थन नहीं करता है, लेकिन मुझे यकीन नहीं है।

क्या कोई तरीका है?

उत्तर

9

तुम भी एक कुंजी दबाएँ और कुंजी रिहाई की घटनाओं के लिए बाध्य और की तरह कुछ कर सकते हैं:

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() 
+0

एक अच्छा विचार है और वैकल्पिक हल की तरह लगता है जब तक माउस बटन के साथ संशोधक कुंजियों के लिए एक उचित समर्थन कार्यान्वित किया जाता है। धन्यवाद विक्टर! – NotNone

0

आप क्यूटी पर कर रहे हैं तो आप बस का उपयोग कर सकते हैं:

def onClick(self, event) 
    # queries the pressed modifier keys 
    mods = QGuiApplication.queryKeyboardModifiers() 
    if event.button == 1 mods == Qt.ShiftModifier: 
     # do something when shift-clicking 
संबंधित मुद्दे