@@ -1251,11 +1251,16 @@ class Event:
1251
1251
guiEvent
1252
1252
The GUI event that triggered the Matplotlib event.
1253
1253
"""
1254
+
1254
1255
def __init__ (self , name , canvas , guiEvent = None ):
1255
1256
self .name = name
1256
1257
self .canvas = canvas
1257
1258
self .guiEvent = guiEvent
1258
1259
1260
+ def process (self ):
1261
+ """Generate an event with name ``self.name`` on ``self.canvas``."""
1262
+ self .canvas .callbacks .process (self .name , self )
1263
+
1259
1264
1260
1265
class DrawEvent (Event ):
1261
1266
"""
@@ -1298,14 +1303,28 @@ class ResizeEvent(Event):
1298
1303
height : int
1299
1304
Height of the canvas in pixels.
1300
1305
"""
1306
+
1301
1307
def __init__ (self , name , canvas ):
1302
1308
super ().__init__ (name , canvas )
1303
1309
self .width , self .height = canvas .get_width_height ()
1304
1310
1311
+ def process (self ):
1312
+ super ().process ()
1313
+ self .canvas .draw_idle ()
1314
+
1305
1315
1306
1316
class CloseEvent (Event ):
1307
1317
"""An event triggered by a figure being closed."""
1308
1318
1319
+ def process (self ):
1320
+ try :
1321
+ super ().process ()
1322
+ except (AttributeError , TypeError ):
1323
+ pass
1324
+ # Suppress AttributeError/TypeError that occur when the python
1325
+ # session is being killed. It may be that a better solution would
1326
+ # be a mechanism to disconnect all callbacks upon shutdown.
1327
+
1309
1328
1310
1329
class LocationEvent (Event ):
1311
1330
"""
@@ -1417,11 +1436,16 @@ class MouseEvent(LocationEvent):
1417
1436
----------
1418
1437
button : None or `MouseButton` or {'up', 'down'}
1419
1438
The button pressed. 'up' and 'down' are used for scroll events.
1439
+
1420
1440
Note that LEFT and RIGHT actually refer to the "primary" and
1421
1441
"secondary" buttons, i.e. if the user inverts their left and right
1422
1442
buttons ("left-handed setting") then the LEFT button will be the one
1423
1443
physically on the right.
1424
1444
1445
+ If this is unset, *name* is "scroll_event", and and *step* is nonzero,
1446
+ then this will be set to "up" or "down" depending on the sign of
1447
+ *step*.
1448
+
1425
1449
key : None or str
1426
1450
The key pressed when the mouse event triggered, e.g. 'shift'.
1427
1451
See `KeyEvent`.
@@ -1459,6 +1483,11 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
1459
1483
"""
1460
1484
if button in MouseButton .__members__ .values ():
1461
1485
button = MouseButton (button )
1486
+ if name == "scroll_event" and button is None :
1487
+ if step > 0 :
1488
+ button = "up"
1489
+ elif step < 0 :
1490
+ button = "down"
1462
1491
self .button = button
1463
1492
self .key = key
1464
1493
self .step = step
@@ -1468,6 +1497,17 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
1468
1497
# 'axes_enter_event', which requires a fully initialized event.
1469
1498
super ().__init__ (name , canvas , x , y , guiEvent = guiEvent )
1470
1499
1500
+ def process (self ):
1501
+ if self .name == "button_press_event" :
1502
+ self .canvas ._button = self .button
1503
+ elif self .name == "button_release_event" :
1504
+ self .canvas ._button = None
1505
+ if self .button is None and self .name != "scroll_event" :
1506
+ self .button = self .canvas ._button
1507
+ if self .key is None :
1508
+ self .key = self .canvas ._key
1509
+ super ().process ()
1510
+
1471
1511
def __str__ (self ):
1472
1512
return (f"{ self .name } : "
1473
1513
f"xy=({ self .x } , { self .y } ) xydata=({ self .xdata } , { self .ydata } ) "
@@ -1508,8 +1548,11 @@ def on_pick(event):
1508
1548
1509
1549
cid = fig.canvas.mpl_connect('pick_event', on_pick)
1510
1550
"""
1551
+
1511
1552
def __init__ (self , name , canvas , mouseevent , artist ,
1512
1553
guiEvent = None , ** kwargs ):
1554
+ if guiEvent is None :
1555
+ guiEvent = mouseevent .guiEvent
1513
1556
super ().__init__ (name , canvas , guiEvent )
1514
1557
self .mouseevent = mouseevent
1515
1558
self .artist = artist
@@ -1550,11 +1593,19 @@ def on_key(event):
1550
1593
1551
1594
cid = fig.canvas.mpl_connect('key_press_event', on_key)
1552
1595
"""
1596
+
1553
1597
def __init__ (self , name , canvas , key , x = 0 , y = 0 , guiEvent = None ):
1554
1598
self .key = key
1555
1599
# super-init deferred to the end: callback errors if called before
1556
1600
super ().__init__ (name , canvas , x , y , guiEvent = guiEvent )
1557
1601
1602
+ def process (self ):
1603
+ if self .name == "key_press_event" :
1604
+ self .canvas ._key = self .key
1605
+ elif self .name == "key_release_event" :
1606
+ self .canvas ._key = None
1607
+ super ().process ()
1608
+
1558
1609
1559
1610
def _get_renderer (figure , print_method = None ):
1560
1611
"""
@@ -1781,12 +1832,14 @@ def blit(self, bbox=None):
1781
1832
def resize (self , w , h ):
1782
1833
"""Set the canvas size in pixels."""
1783
1834
1835
+ @cbook .deprecated ("3.3" , alternative = "DrawEvent(...).process()" )
1784
1836
def draw_event (self , renderer ):
1785
1837
"""Pass a `DrawEvent` to all functions connected to ``draw_event``."""
1786
1838
s = 'draw_event'
1787
1839
event = DrawEvent (s , self , renderer )
1788
1840
self .callbacks .process (s , event )
1789
1841
1842
+ @cbook .deprecated ("3.3" , alternative = "ResizeEvent(...).process()" )
1790
1843
def resize_event (self ):
1791
1844
"""
1792
1845
Pass a `ResizeEvent` to all functions connected to ``resize_event``.
@@ -1796,6 +1849,7 @@ def resize_event(self):
1796
1849
self .callbacks .process (s , event )
1797
1850
self .draw_idle ()
1798
1851
1852
+ @cbook .deprecated ("3.3" , alternative = "CloseEvent(...).process()" )
1799
1853
def close_event (self , guiEvent = None ):
1800
1854
"""
1801
1855
Pass a `CloseEvent` to all functions connected to ``close_event``.
@@ -1812,6 +1866,7 @@ def close_event(self, guiEvent=None):
1812
1866
# AttributeError occurs on OSX with qt4agg upon exiting
1813
1867
# with an open window; 'callbacks' attribute no longer exists.
1814
1868
1869
+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
1815
1870
def key_press_event (self , key , guiEvent = None ):
1816
1871
"""
1817
1872
Pass a `KeyEvent` to all functions connected to ``key_press_event``.
@@ -1822,6 +1877,7 @@ def key_press_event(self, key, guiEvent=None):
1822
1877
s , self , key , self ._lastx , self ._lasty , guiEvent = guiEvent )
1823
1878
self .callbacks .process (s , event )
1824
1879
1880
+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
1825
1881
def key_release_event (self , key , guiEvent = None ):
1826
1882
"""
1827
1883
Pass a `KeyEvent` to all functions connected to ``key_release_event``.
@@ -1832,6 +1888,7 @@ def key_release_event(self, key, guiEvent=None):
1832
1888
self .callbacks .process (s , event )
1833
1889
self ._key = None
1834
1890
1891
+ @cbook .deprecated ("3.3" , alternative = "PickEvent(...).process()" )
1835
1892
def pick_event (self , mouseevent , artist , ** kwargs ):
1836
1893
"""
1837
1894
Callback processing for pick events.
@@ -1845,6 +1902,7 @@ def pick_event(self, mouseevent, artist, **kwargs):
1845
1902
** kwargs )
1846
1903
self .callbacks .process (s , event )
1847
1904
1905
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1848
1906
def scroll_event (self , x , y , step , guiEvent = None ):
1849
1907
"""
1850
1908
Callback processing for scroll events.
@@ -1865,6 +1923,7 @@ def scroll_event(self, x, y, step, guiEvent=None):
1865
1923
step = step , guiEvent = guiEvent )
1866
1924
self .callbacks .process (s , mouseevent )
1867
1925
1926
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1868
1927
def button_press_event (self , x , y , button , dblclick = False , guiEvent = None ):
1869
1928
"""
1870
1929
Callback processing for mouse button press events.
@@ -1882,6 +1941,7 @@ def button_press_event(self, x, y, button, dblclick=False, guiEvent=None):
1882
1941
dblclick = dblclick , guiEvent = guiEvent )
1883
1942
self .callbacks .process (s , mouseevent )
1884
1943
1944
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1885
1945
def button_release_event (self , x , y , button , guiEvent = None ):
1886
1946
"""
1887
1947
Callback processing for mouse button release events.
@@ -1906,6 +1966,7 @@ def button_release_event(self, x, y, button, guiEvent=None):
1906
1966
self .callbacks .process (s , event )
1907
1967
self ._button = None
1908
1968
1969
+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
1909
1970
def motion_notify_event (self , x , y , guiEvent = None ):
1910
1971
"""
1911
1972
Callback processing for mouse movement events.
@@ -1931,6 +1992,7 @@ def motion_notify_event(self, x, y, guiEvent=None):
1931
1992
guiEvent = guiEvent )
1932
1993
self .callbacks .process (s , event )
1933
1994
1995
+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
1934
1996
def leave_notify_event (self , guiEvent = None ):
1935
1997
"""
1936
1998
Callback processing for the mouse cursor leaving the canvas.
@@ -1947,6 +2009,7 @@ def leave_notify_event(self, guiEvent=None):
1947
2009
LocationEvent .lastevent = None
1948
2010
self ._lastx , self ._lasty = None , None
1949
2011
2012
+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
1950
2013
def enter_notify_event (self , guiEvent = None , xy = None ):
1951
2014
"""
1952
2015
Callback processing for the mouse cursor entering the canvas.
0 commit comments