Skip to content

Commit cb8969f

Browse files
authored
Merge pull request matplotlib#12640 from anntzer/mousebutton
Introduce MouseButton enum for MouseEvent.
2 parents d42a0a8 + 6cc4e4b commit cb8969f

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 logging
@@ -1367,6 +1368,12 @@ def _update_enter_leave(self):
13671368
LocationEvent.lastevent = self
13681369

13691370

1371+
class MouseButton(IntEnum):
1372+
LEFT = 1
1373+
MIDDLE = 2
1374+
RIGHT = 3
1375+
1376+
13701377
class MouseEvent(LocationEvent):
13711378
"""
13721379
A mouse event ('button_press_event',
@@ -1379,21 +1386,26 @@ class MouseEvent(LocationEvent):
13791386
13801387
Attributes
13811388
----------
1382-
button : {None, 1, 2, 3, 'up', 'down'}
1389+
button : {None, MouseButton.LEFT, MouseButton.MIDDLE, MouseButton.RIGHT, \
1390+
'up', 'down'}
13831391
The button pressed. 'up' and 'down' are used for scroll events.
13841392
Note that in the nbagg backend, both the middle and right clicks
1385-
return 3 since right clicking will bring up the context menu in
1393+
return RIGHT since right clicking will bring up the context menu in
13861394
some browsers.
1395+
Note that LEFT and RIGHT actually refer to the "primary" and
1396+
"secondary" buttons, i.e. if the user inverts their left and right
1397+
buttons ("left-handed setting") then the LEFT button will be the one
1398+
physically on the right.
13871399
13881400
key : None or str
13891401
The key pressed when the mouse event triggered, e.g. 'shift'.
13901402
See `KeyEvent`.
13911403
13921404
step : scalar
1393-
The Number of scroll steps (positive for 'up', negative for 'down').
1405+
The number of scroll steps (positive for 'up', negative for 'down').
13941406
13951407
dblclick : bool
1396-
*True* if the event is a double-click.
1408+
Whether the event is a double-click.
13971409
13981410
Examples
13991411
--------
@@ -1412,6 +1424,8 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
14121424
button pressed None, 1, 2, 3, 'up', 'down'
14131425
"""
14141426
LocationEvent.__init__(self, name, canvas, x, y, guiEvent=guiEvent)
1427+
if button in MouseButton.__members__.values():
1428+
button = MouseButton(button)
14151429
self.button = button
14161430
self.key = key
14171431
self.step = step

0 commit comments

Comments
 (0)