@@ -1191,11 +1191,16 @@ class Event:
11911191 guiEvent
11921192 The GUI event that triggered the Matplotlib event.
11931193 """
1194+
11941195 def __init__ (self , name , canvas , guiEvent = None ):
11951196 self .name = name
11961197 self .canvas = canvas
11971198 self .guiEvent = guiEvent
11981199
1200+ def process (self ):
1201+ """Generate an event with name ``self.name`` on ``self.canvas``."""
1202+ self .canvas .callbacks .process (self .name , self )
1203+
11991204
12001205class DrawEvent (Event ):
12011206 """
@@ -1238,14 +1243,28 @@ class ResizeEvent(Event):
12381243 height : int
12391244 Height of the canvas in pixels.
12401245 """
1246+
12411247 def __init__ (self , name , canvas ):
12421248 Event .__init__ (self , name , canvas )
12431249 self .width , self .height = canvas .get_width_height ()
12441250
1251+ def process (self ):
1252+ super ().process ()
1253+ self .canvas .draw_idle ()
1254+
12451255
12461256class CloseEvent (Event ):
12471257 """An event triggered by a figure being closed."""
12481258
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+
12491268
12501269class LocationEvent (Event ):
12511270 """
@@ -1360,11 +1379,16 @@ class MouseEvent(LocationEvent):
13601379 Note that in the nbagg backend, both the middle and right clicks
13611380 return RIGHT since right clicking will bring up the context menu in
13621381 some browsers.
1382+
13631383 Note that LEFT and RIGHT actually refer to the "primary" and
13641384 "secondary" buttons, i.e. if the user inverts their left and right
13651385 buttons ("left-handed setting") then the LEFT button will be the one
13661386 physically on the right.
13671387
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+
13681392 key : None or str
13691393 The key pressed when the mouse event triggered, e.g. 'shift'.
13701394 See `KeyEvent`.
@@ -1403,11 +1427,27 @@ def __init__(self, name, canvas, x, y, button=None, key=None,
14031427 LocationEvent .__init__ (self , name , canvas , x , y , guiEvent = guiEvent )
14041428 if button in MouseButton .__members__ .values ():
14051429 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"
14061435 self .button = button
14071436 self .key = key
14081437 self .step = step
14091438 self .dblclick = dblclick
14101439
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+
14111451 def __str__ (self ):
14121452 return (f"{ self .name } : "
14131453 f"xy=({ self .x } , { self .y } ) xydata=({ self .xdata } , { self .ydata } ) "
@@ -1448,8 +1488,11 @@ def on_pick(event):
14481488
14491489 cid = fig.canvas.mpl_connect('pick_event', on_pick)
14501490 """
1491+
14511492 def __init__ (self , name , canvas , mouseevent , artist ,
14521493 guiEvent = None , ** kwargs ):
1494+ if guiEvent is None :
1495+ guiEvent = mouseevent .guiEvent
14531496 Event .__init__ (self , name , canvas , guiEvent )
14541497 self .mouseevent = mouseevent
14551498 self .artist = artist
@@ -1490,10 +1533,18 @@ def on_key(event):
14901533
14911534 cid = fig.canvas.mpl_connect('key_press_event', on_key)
14921535 """
1536+
14931537 def __init__ (self , name , canvas , key , x = 0 , y = 0 , guiEvent = None ):
14941538 LocationEvent .__init__ (self , name , canvas , x , y , guiEvent = guiEvent )
14951539 self .key = key
14961540
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+
14971548
14981549def _get_renderer (figure , print_method , * , draw_disabled = False ):
14991550 """
@@ -1651,12 +1702,14 @@ def blit(self, bbox=None):
16511702 def resize (self , w , h ):
16521703 """Set the canvas size in pixels."""
16531704
1705+ @cbook .deprecated ("3.3" , alternative = "DrawEvent(...).process()" )
16541706 def draw_event (self , renderer ):
16551707 """Pass a `DrawEvent` to all functions connected to ``draw_event``."""
16561708 s = 'draw_event'
16571709 event = DrawEvent (s , self , renderer )
16581710 self .callbacks .process (s , event )
16591711
1712+ @cbook .deprecated ("3.3" , alternative = "ResizeEvent(...).process()" )
16601713 def resize_event (self ):
16611714 """
16621715 Pass a `ResizeEvent` to all functions connected to ``resize_event``.
@@ -1666,6 +1719,7 @@ def resize_event(self):
16661719 self .callbacks .process (s , event )
16671720 self .draw_idle ()
16681721
1722+ @cbook .deprecated ("3.3" , alternative = "CloseEvent(...).process()" )
16691723 def close_event (self , guiEvent = None ):
16701724 """
16711725 Pass a `CloseEvent` to all functions connected to ``close_event``.
@@ -1682,6 +1736,7 @@ def close_event(self, guiEvent=None):
16821736 # AttributeError occurs on OSX with qt4agg upon exiting
16831737 # with an open window; 'callbacks' attribute no longer exists.
16841738
1739+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
16851740 def key_press_event (self , key , guiEvent = None ):
16861741 """
16871742 Pass a `KeyEvent` to all functions connected to ``key_press_event``.
@@ -1692,6 +1747,7 @@ def key_press_event(self, key, guiEvent=None):
16921747 s , self , key , self ._lastx , self ._lasty , guiEvent = guiEvent )
16931748 self .callbacks .process (s , event )
16941749
1750+ @cbook .deprecated ("3.3" , alternative = "KeyEvent(...).process()" )
16951751 def key_release_event (self , key , guiEvent = None ):
16961752 """
16971753 Pass a `KeyEvent` to all functions connected to ``key_release_event``.
@@ -1702,6 +1758,7 @@ def key_release_event(self, key, guiEvent=None):
17021758 self .callbacks .process (s , event )
17031759 self ._key = None
17041760
1761+ @cbook .deprecated ("3.3" , alternative = "PickEvent(...).process()" )
17051762 def pick_event (self , mouseevent , artist , ** kwargs ):
17061763 """
17071764 Callback processing for pick events.
@@ -1715,6 +1772,7 @@ def pick_event(self, mouseevent, artist, **kwargs):
17151772 ** kwargs )
17161773 self .callbacks .process (s , event )
17171774
1775+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
17181776 def scroll_event (self , x , y , step , guiEvent = None ):
17191777 """
17201778 Callback processing for scroll events.
@@ -1735,6 +1793,7 @@ def scroll_event(self, x, y, step, guiEvent=None):
17351793 step = step , guiEvent = guiEvent )
17361794 self .callbacks .process (s , mouseevent )
17371795
1796+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
17381797 def button_press_event (self , x , y , button , dblclick = False , guiEvent = None ):
17391798 """
17401799 Callback processing for mouse button press events.
@@ -1752,6 +1811,7 @@ def button_press_event(self, x, y, button, dblclick=False, guiEvent=None):
17521811 dblclick = dblclick , guiEvent = guiEvent )
17531812 self .callbacks .process (s , mouseevent )
17541813
1814+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
17551815 def button_release_event (self , x , y , button , guiEvent = None ):
17561816 """
17571817 Callback processing for mouse button release events.
@@ -1776,6 +1836,7 @@ def button_release_event(self, x, y, button, guiEvent=None):
17761836 self .callbacks .process (s , event )
17771837 self ._button = None
17781838
1839+ @cbook .deprecated ("3.3" , alternative = "MouseEvent(...).process()" )
17791840 def motion_notify_event (self , x , y , guiEvent = None ):
17801841 """
17811842 Callback processing for mouse movement events.
@@ -1801,6 +1862,7 @@ def motion_notify_event(self, x, y, guiEvent=None):
18011862 guiEvent = guiEvent )
18021863 self .callbacks .process (s , event )
18031864
1865+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
18041866 def leave_notify_event (self , guiEvent = None ):
18051867 """
18061868 Callback processing for the mouse cursor leaving the canvas.
@@ -1817,6 +1879,7 @@ def leave_notify_event(self, guiEvent=None):
18171879 LocationEvent .lastevent = None
18181880 self ._lastx , self ._lasty = None , None
18191881
1882+ @cbook .deprecated ("3.3" , alternative = "LocationEvent(...).process()" )
18201883 def enter_notify_event (self , guiEvent = None , xy = None ):
18211884 """
18221885 Callback processing for the mouse cursor entering the canvas.
0 commit comments