2013-08-09 48 views
6

Tôi có một matplotlib và tôi đã tạo ra một button_press_event như thế này:matplotlib: Làm thế nào để nhận được sự thay đổi click trên hình?

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 

Bây giờ là nó có thể thay đổi điều khiển bánh xe nhấp chuột để một cái gì đó như thế này

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) 

Dường như button_press_event không các phím hỗ trợ và key_press_event không hỗ trợ nhấp chuột vào nút chuột, nhưng tôi không chắc chắn.

Có cách nào không?

Trả lời

9

Bạn cũng có thể ràng buộc một phím bấm và các sự kiện phát hành chủ chốt và làm điều gì đó như:

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 

Sau đó, bạn có thể kiểm tra trong onClick chức năng của bạn nếu self.shift_is_held.

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

Có vẻ như là một ý tưởng tốt và cách giải quyết cho đến khi một sự hỗ trợ thích hợp cho các phím bổ với các nút chuột được thực hiện. Cảm ơn Viktor! – NotNone

0

Nếu bạn đang ở trên Qt sau đó bạn chỉ có thể sử dụng:

def onClick(self, event) 
    # queries the pressed modifier keys 
    mods = QGuiApplication.queryKeyboardModifiers() 
    if event.button == 1 mods == Qt.ShiftModifier: 
     # do something when shift-clicking