8000 Introduce MouseButton enum for MouseEvent. · matplotlib/matplotlib@6cc4e4b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cc4e4b

Browse files
committed
Introduce MouseButton enum for MouseEvent.
MouseButton.LEFT/MIDDLE/RIGHT is a bit less cryptic than 1/2/3 (which remain available as MouseButton is an IntEnum). The names LEFT/MIDDLE/RIGHT are used by both Qt and wx, with the same inversion for left-ha 10000 nded mode as noted in the docstring. (Gtk uses PRIMARY/MIDDLE/SECONDARY, which works better for left-handed mode but seems otherwise less readable; tk just uses 1/2/3). Changed one example to demonstrate the use; I can change the other examples if we agree on the general approach. (That specific example printed mouse coordinates both on move and on click, which seems a bit redundant, so I moved the "disconnect" part to the click handler.)
1 parent 921d4f4 commit 6cc4e4b

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MouseEvent button attribute is now an IntEnum
2+
`````````````````````````````````````````````
3+
4+
The :attr:`button` attribute of `~.MouseEvent` instances can take the values
5+
None, 1 (left button), 2 (middle button), 3 (right button), "up" (scroll), and
6+
"down" (scroll). For better legibility, the 1, 2, and 3 values are now
7+
represented using the `IntEnum` class `matplotlib.backend_bases.MouseButton`,
8+
with the values `MouseButton.LEFT` (``== 1``), `MouseButton.MIDDLE` (``== 2``),
9+
and `MouseButton.RIGHT` (``== 3``).
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""
22
===========
3-
Coords Demo
3+
Coords demo
44
===========
55
6-
An example of how to interact with the plotting canvas by connecting
7-
to move and click events
6+
An example of how to interact with the plotting canvas by connecting to move
7+
and click events.
88
"""
9-
import sys
9+
10+
from matplotlib.backend_bases import MouseButton
1011
import matplotlib.pyplot as plt
1112
import numpy as np
1213

@@ -19,25 +20,18 @@
1920
def on_move(event):
2021
# get the x and y pixel coords
2122
x, y = event.x, event.y
22-
2323
if event.inaxes:
2424
ax = event.inaxes # the axes instance
2525
print('data coords %f %f' % (event.xdata, event.ydata))
2626

2727

2828
def on_click(event):
29-
# get the x and y coords, flip y from top to bottom
30-
x, y = event.x, event.y
31-
if event.button == 1:
32-
if event.inaxes is not None:
33-
print('data coords %f %f' % (event.xdata, event.ydata))
29+
if event.button is MouseButton.LEFT:
30+
print('disconnecting callback')
31+
plt.disconnect(binding_id)
3432

3533

3634
binding_id = plt.connect('motion_notify_event', on_move)
3735
plt.connect('button_press_event', on_click)
3836

39-
if "test_disconnect" in sys.argv:
40-
print("disconnecting console coordinate printout...")
41-
plt.disconnect(binding_id)
42-
4337
plt.show()

lib/matplotlib/backend_bases.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"""
3434

3535
from contextlib import contextmanager
36+
from enum import IntEnum
3637
import importlib
3738
import io
3839
import os
@@ -1425,6 +1426,12 @@ def _update_enter_leave(self):
14251426
LocationEvent.lastevent = self
14261427

14271428

1429+
class MouseButton(IntEnum):
1430+
LEFT = 1
1431+
MIDDLE = 2
1432+
RIGHT = 3
1433+
1434+
14281435
class MouseEvent(LocationEvent):
14291436
"""
14301437
A mouse event ('button_press_event',
@@ -1437,21 +1444,26 @@ class MouseEvent(LocationEvent):
14371444
14381445
Attributes
14391446
----------
1440-
button : {None, 1, 2, 3, 'up', 'down'}
1447+
button : {None, MouseButton.LEFT, MouseButton.MIDDLE, MouseButton.RIGHT, \
1448+
'up', 'down'}
14411449
The button pressed. 'up' and 'down' are used for scroll events.
14421450
Note that in the nbagg backend, both the middle and right clicks
1443-
return 3 since right clicking will bring up the context menu in
1451+
return RIGHT since right clicking will bring up the context menu in
14441452
some browsers.
1453+
Note that LEFT and RIGHT actually refer to the "primary" and
1454+
"secondary" buttons, i.e. if the user inverts their left and right
1455+
buttons ("left-handed setting") then the LEFT button will be the one
1456+
physically on the right.
14451457
14461458
key : None or str
14471459
The key pressed when the mouse event triggered, e.g. 'shift'.
14481460
See `KeyEvent`.
14491461
14501462
step : scalar
1451-
The Number of scroll steps (positive for 'up', negative for 'down').
1463+
The number of scroll steps (positive for 'up', negative for 'down').
14521464
14531465
dblclick : bool
1454-
*True* if the event is a double-click.
1466+
Whether the event is a double-click.
14551467
14561468
Examples
14571469
--------
@@ -1470,6 +1482,8 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
14701482
button pressed None, 1, 2, 3, 'up', 'down'
14711483
"""
14721484
LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent)
1485+
if button in MouseButton.__members__.values():
1486+
button = MouseButton(button)
14731487
self.button = button
14741488
self.key = key
14751489
self.step = step

0 commit comments

Comments
 (0)
0