2222import numpy as np
2323
2424import matplotlib as mpl
25- from matplotlib import docstring , projections
25+ from matplotlib import _blocking_input , docstring , projections
2626from matplotlib .artist import (
2727 Artist , allow_rasterization , _finalize_rasterization )
2828from matplotlib .backend_bases import (
3333import matplotlib .image as mimage
3434
3535from matplotlib .axes import Axes , SubplotBase , subplot_class_factory
36- from matplotlib .blocking_input import BlockingMouseInput , BlockingKeyMouseInput
3736from matplotlib .gridspec import GridSpec
3837import matplotlib .legend as mlegend
3938from matplotlib .patches import Rectangle
@@ -3013,12 +3012,52 @@ def ginput(self, n=1, timeout=30, show_clicks=True,
30133012 terminates input and any other key (not already used by the window
30143013 manager) selects a point.
30153014 """
3016- blocking_mouse_input = BlockingMouseInput (self ,
3017- mouse_add = mouse_add ,
3018- mouse_pop = mouse_pop ,
3019- mouse_stop = mouse_stop )
3020- return blocking_mouse_input (n = n , timeout = timeout ,
3021- show_clicks = show_clicks )
3015+ clicks = []
3016+ marks = []
3017+
3018+ def handler (event ):
3019+ is_button = event .name == "button_press_event"
3020+ is_key = event .name == "key_press_event"
3021+ # Quit (even if not in infinite mode; this is consistent with
3022+ # MATLAB and sometimes quite useful, but will require the user to
3023+ # test how many points were actually returned before using data).
3024+ if (is_button and event .button == mouse_stop
3025+ or is_key and event .key in ["escape" , "enter" ]):
3026+ self .canvas .stop_event_loop ()
3027+ # Pop last click.
3028+ elif (is_button and event .button == mouse_pop
3029+ or is_key and event .key in ["backspace" , "delete" ]):
3030+ if clicks :
3031+ clicks .pop ()
3032+ if show_clicks :
3033+ marks .pop ().remove ()
3034+ self .canvas .draw ()
3035+ # Add new click.
3036+ elif (is_button and event .button == mouse_add
3037+ # On macOS/gtk, some keys return None.
3038+ or is_key and event .key is not None ):
3039+ if event .inaxes :
3040+ clicks .append ((event .xdata , event .ydata ))
3041+ _log .info ("input %i: %f, %f" ,
3042+ len (clicks ), event .xdata , event .ydata )
3043+ if show_clicks :
3044+ line = mpl .lines .Line2D ([event .xdata ], [event .ydata ],
3045+ marker = "+" , color = "r" )
3046+ event .inaxes .add_line (line )
3047+ marks .append (line )
3048+ self .canvas .draw ()
3049+ if len (clicks ) == n and n > 0 :
3050+ self .canvas .stop_event_loop ()
3051+
3052+ _blocking_input .blocking_input_loop (
3053+ self , ["button_press_event" , "key_press_event" ], timeout , handler )
3054+
3055+ # Cleanup.
3056+ for mark in marks :
3057+ mark .remove ()
3058+ self .canvas .draw ()
3059+
3060+ return clicks
30223061
30233062 def waitforbuttonpress (self , timeout = - 1 ):
30243063 """
@@ -3028,8 +3067,17 @@ def waitforbuttonpress(self, timeout=-1):
30283067 mouse button was pressed and None if no input was given within
30293068 *timeout* seconds. Negative values deactivate *timeout*.
30303069 """
3031- blocking_input = BlockingKeyMouseInput (self )
3032- return blocking_input (timeout = timeout )
3070+ event = None
3071+
3072+ def handler (ev ):
3073+ nonlocal event
3074+ event = ev
3075+ self .canvas .stop_event_loop ()
3076+
3077+ _blocking_input .blocking_input_loop (
3078+ self , ["button_press_event" , "key_press_event" ], timeout , handler )
3079+
3080+ return None if event is None else event .name == "key_press_event"
30333081
30343082 def init_layoutgrid (self ):
30353083 """Initialize the layoutgrid for use in constrained_layout."""
0 commit comments