@@ -1191,11 +1191,16 @@ class Event:
1191
1191
guiEvent
1192
1192
The GUI event that triggered the Matplotlib event.
1193
1193
"""
1194
+
1194
1195
def __init__ (self , name , canvas , guiEvent = None ):
1195
1196
self .name = name
1196
1197
self .canvas = canvas
1197
1198
self .guiEvent = guiEvent
1198
1199
1200
+ def process (self ):
1201
+ """Generate an event with name ``self.name`` on ``self.canvas``."""
1202
+ self .canvas .callbacks .process (self .name , self )
1203
+
1199
1204
1200
1205
class DrawEvent (Event ):
1201
1206
"""
@@ -1238,14 +1243,28 @@ class ResizeEvent(Event):
1238
1243
height : int
1239
1244
Height of the canvas in pixels.
1240
1245
"""
1246
+
1241
1247
def __init__ (self , name , canvas ):
1242
1248
Event .__init__ (self , name , canvas )
1243
1249
self .width , self .height = canvas .get_width_height ()
1244
1250
1251
+ def process (self ):
1252
+ super ().process ()
1253
+ self .canvas .draw_idle ()
1254
+
1245
1255
1246
1256
class CloseEvent (Event ):
1247
1257
"""An event triggered by a figure being closed."""
1248
1258
1259
+ def process (self ):
1260
+ try :
1261
+ super ().process ()
1262
+ except (AttributeError , TypeError ):
1263
+ pass
1264
+ # Suppress AttributeError/TypeError that occur when the python
1265
+ # session is being killed. It may be that a better solution would
1266
+ # be a mechanism to disconnect all callbacks upon shutdown.
1267
+
1249
1268
1250
1269
class LocationEvent (Event ):
1251
1270
"""
@@ -1360,11 +1379,16 @@ class MouseEvent(LocationEvent):
1360
1379
Note that in the nbagg backend, both the middle and right clicks
1361
1380
return RIGHT since right clicking will bring up the context menu in
1362
1381
some browsers.
1382
+
1363
1383
Note that LEFT and RIGHT actually refer to the "primary" and
1364
1384
"secondary" buttons, i.e. if the user inverts their left and right
1365
1385
buttons ("left-handed setting") then the LEFT button will be the one
1366
1386
physically on the right.
1367
1387
1388
+ If this is unset, *name* is "scroll_event", and and *step* is nonzero,
1389
+ then this will be set to "up" or "down" depending on the sign of
1390
+ *step*.
1391
+
1368
1392
key : None or str
1369
1393
The key pressed when the mouse event triggered, e.g. 'shift'.
1370
1394
See `KeyEvent`.
@@ -1403,11 +1427,27 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
1403
1427
LocationEvent .__init__ (self , name , canvas , x , y , guiEvent = guiEvent )
1404
1428
if button in MouseButton .__members__ .values ():
1405
1429
button = MouseButton (button )
1430
+ if name == "scroll_event" and button is None :
1431
+ if step > 0 :
1432
+ button = "up"
1433
+ elif step < 0 :
1434
+ button = "down"
1406
1435
self .button = button
1407
1436
self .key = key
1408
1437
self .step = step
1409
1438
self .dblclick = dblclick
1410
1439
1440
+ def process (self ):
1441
+ if self .name == "button_press_event" :
1442
+ self .canvas ._button = self .button
1443
+ elif self .name == "button_release_event" :
1444
+ self .canvas ._button = None
1445
+ if self .button is None and self .name != "scroll_event" :
1446
+ self .button = self .canvas ._button
1447
+ if self .key is None :
1448
+ self .key = self .canvas ._key
1449
+ super ().process ()
1450
+
1411
1451
def __str__ (self ):
1412
1452
return (f"{ self .name } : "
1413
1453
f"xy=({ self .x } , { self .y } ) xydata=({ self .xdata } , { self .ydata } ) "
@@ -1448,8 +1488,11 @@ def on_pick(event):
1448
1488
1449
1489
cid = fig.canvas.mpl_connect('pick_event', on_pick)
1450
1490
"""
1491
+
1451
1492
def __init__ (self , name , canvas , mouseevent , artist ,
1452
1493
guiEvent = None , ** kwargs ):
1494
+ if guiEvent is None :
1495
+ guiEvent = mouseevent .guiEvent
1453
1496
Event .__init__ (self , name , canvas , guiEvent )
1454
1497
self .mouseevent = mouseevent
1455
1498
self .artist = artist
@@ -1490,10 +1533,18 @@ def on_key(event):
1490
1533
1491
1534
cid = fig.canvas.mpl_connect('key_press_event', on_key)
1492
1535
"""
1536
+
1493
1537
def __init__ (self , name , canvas , key , x = 0 , y = 0 , guiEvent = None ):
1494
1538
LocationEvent .__init__ (self , name , canvas , x , y , guiEvent = guiEvent )
1495
1539
self .key = key
1496
1540
1541
+ def process (self ):
1542
+ if self .name == "key_press_event" :
1543
+ self .canvas ._key = self .key
1544
+ elif self .name == "key_release_event" :
1545
+ self .canvas ._key = None
1546
+ super ().process ()
1547
+
1497
1548
1498
1549
def _get_renderer (figure , print_method , * , draw_disabled = False ):
1499
1550
"""
@@ -1651,12 +1702,14 @@ def blit(self, bbox=None):
1651
1702
def resize (self , w , h ):
1652
1703
"""Set the canvas size in pixels."""
1653
1704
1705
+ @cbook .deprecated ("3.3" , alternative = "DrawEvent(...).process()" )
1654
1706
def draw_event (self , renderer ):
1655
1707
"""Pass a `DrawEvent` to all functions connected to ``draw_event``."""
1656
1708
s = 'draw_event'
1657
1709
event = DrawEvent (s , self , renderer )
1658
1710
self .callbacks .process (s , event )
1659
1711
1712
+ @cbook .deprecated ("3.3" , alternative = "ResizeEvent(...).process()" )
1660
1713
def resize_event (self ):
1661
1714
"""
1662
1715
Pass a `ResizeEvent` to all functions connected to ``resize_event``.
@@ -1666,6 +1719,7 @@ def resize_event(self):
1666
1719
self .callbacks .process (s , event )
1667
1720
self .draw_idle ()
1668
1721
1722
+ @cbook .deprecated ("3.3" , alternative = "CloseEvent(...).process()" )
1669
1723
def close_event (self , guiEvent = None ):
1670
1724
"""
1671
1725
Pass a `CloseEvent` to all functions connected to ``close_event``.
@@ -1682,6 +1736,7 @@ def close_event(self, guiEvent=None):
1682
1736
# AttributeError occurs on OSX with qt4agg upon exiting
1683
1737
# with an open window; 'callbacks' attribute no longer exists.
1684
1738
1739
+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
1685
1740
def key_press_event (self , key , guiEvent = None ):
1686
1741
"""
1687
1742
Pass a `KeyEvent` to all functions connected to ``key_press_event``.
@@ -1692,6 +1747,7 @@ def key_press_event(self, key, guiEvent=None):
1692
1747
s , self , key , self ._lastx , self ._lasty , guiEvent = guiEvent )
1693
1748
self .callbacks .process (s , event )
1694
1749
1750
+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
1695
1751
def key_release_event (self , key , guiEvent = None ):
1696
1752
"""
1697
1753
Pass a `KeyEvent` to all functions connected to ``key_release_event``.
@@ -1702,6 +1758,7 @@ def key_release_event(self, key, guiEvent=None):
1702
1758
self .callbacks .process (s , event )
1703
1759
self ._key = None
1704
1760
1761
+ @cbook .deprecated ("3.3" , alternative = "PickEvent(...).process()" )
1705
1762
def pick_event (self , mouseevent , artist , ** kwargs ):
1706
1763
"""
1707
1764
Callback processing for pick events.
@@ -1715,6 +1772,7 @@ def pick_event(self, mouseevent, artist, **kwargs):
1715
1772
** kwargs )
1716
1773
self .callbacks .process (s , event )
1717
1774
1775
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1718
1776
def scroll_event (self , x , y , step , guiEvent = None ):
1719
1777
"""
1720
1778
Callback processing for scroll events.
@@ -1735,6 +1793,7 @@ def scroll_event(self, x, y, step, guiEvent=None):
1735
1793
step = step , guiEvent = guiEvent )
1736
1794
self .callbacks .process (s , mouseevent )
1737
1795
1796
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1738
1797
def button_press_event (self , x , y , button , dblclick = False , guiEvent = None ):
1739
1798
"""
1740
1799
Callback processing for mouse button press events.
@@ -1752,6 +1811,7 @@ def button_press_event(self, x, y, button, dblclick=False, guiEvent=None):
1752
1811
dblclick = dblclick , guiEvent = guiEvent )
1753
1812
self .callbacks .process (s , mouseevent )
1754
1813
1814
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1755
1815
def button_release_event (self , x , y , button , guiEvent = None ):
1756
1816
"""
1757
1817
Callback processing for mouse button release events.
@@ -1776,6 +1836,7 @@ def button_release_event(self, x, y, button, guiEvent=None):
1776
1836
self .callbacks .process (s , event )
1777
1837
self ._button = None
1778
1838
1839
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1779
1840
def motion_notify_event (self , x , y , guiEvent = None ):
1780
1841
"""
1781
1842
Callback processing for mouse movement events.
@@ -1801,6 +1862,7 @@ def motion_notify_event(self, x, y, guiEvent=None):
1801
1862
guiEvent = guiEvent )
1802
1863
self .callbacks .process (s , event )
1803
1864
1865
+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
1804
1866
def leave_notify_event (self , guiEvent = None ):
1805
1867
"""
1806
1868
Callback processing for the mouse cursor leaving the canvas.
@@ -1817,6 +1879,7 @@ def leave_notify_event(self, guiEvent=None):
1817
1879
LocationEvent .lastevent = None
1818
1880
self ._lastx , self ._lasty = None , None
1819
1881
1882
+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
1820
1883
def enter_notify_event (self , guiEvent = None , xy = None ):
1821
1884
"""
1822
1885
Callback processing for the mouse cursor entering the canvas.
0 commit comments