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
@@ -3027,12 +3026,52 @@ def ginput(self, n=1, timeout=30, show_clicks=True,
30273026 terminates input and any other key (not already used by the window
30283027 manager) selects a point.
30293028 """
3030- blocking_mouse_input = BlockingMouseInput (self ,
3031- mouse_add = mouse_add ,
3032- mouse_pop = mouse_pop ,
3033- mouse_stop = mouse_stop )
3034- return blocking_mouse_input (n = n , timeout = timeout ,
3035- show_clicks = show_clicks )
3029+ clicks = []
3030+ marks = []
3031+
3032+ def handler (event ):
3033+ is_button = event .name == "button_press_event"
3034+ is_key = event .name == "key_press_event"
3035+ # Quit (even if not in infinite mode; this is consistent with
3036+ # MATLAB and sometimes quite useful, but will require the user to
3037+ # test how many points were actually returned before using data).
3038+ if (is_button and event .button == mouse_stop
3039+ or is_key and event .key in ["escape" , "enter" ]):
3040+ self .canvas .stop_event_loop ()
3041+ # Pop last click.
3042+ elif (is_button and event .button == mouse_pop
3043+ or is_key and event .key in ["backspace" , "delete" ]):
3044+ if clicks :
3045+ clicks .pop ()
3046+ if show_clicks :
3047+ marks .pop ().remove ()
3048+ self .canvas .draw ()
3049+ # Add new click.
3050+ elif (is_button and event .button == mouse_add
3051+ # On macOS/gtk, some keys return None.
3052+ or is_key and event .key is not None ):
3053+ if event .inaxes :
3054+ clicks .append ((event .xdata , event .ydata ))
3055+ _log .info ("input %i: %f, %f" ,
3056+ len (clicks ), event .xdata , event .ydata )
3057+ if show_clicks :
3058+ line = mpl .lines .Line2D ([event .xdata ], [event .ydata ],
3059+ marker = "+" , color = "r" )
3060+ event .inaxes .add_line (line )
3061+ marks .append (line )
3062+ self .canvas .draw ()
3063+ if len (clicks ) == n and n > 0 :
3064+ self .canvas .stop_event_loop ()
3065+
3066+ _blocking_input .blocking_input_loop (
3067+ self , ["button_press_event" , "key_press_event" ], timeout , handler )
3068+
3069+ # Cleanup.
3070+ for mark in marks :
3071+ mark .remove ()
3072+ self .canvas .draw ()
3073+
3074+ return clicks
30363075
30373076 def waitforbuttonpress (self , timeout = - 1 ):
30383077 """
@@ -3042,8 +3081,17 @@ def waitforbuttonpress(self, timeout=-1):
30423081 mouse button was pressed and None if no input was given within
30433082 *timeout* seconds. Negative values deactivate *timeout*.
30443083 """
3045- blocking_input = BlockingKeyMouseInput (self )
3046- return blocking_input (timeout = timeout )
3084+ event = None
3085+
3086+ def handler (ev ):
3087+ nonlocal event
3088+ event = ev
3089+ self .canvas .stop_event_loop ()
3090+
3091+ _blocking_input .blocking_input_loop (
3092+ self , ["button_press_event" , "key_press_event" ], timeout , handler )
3093+
3094+ return None if event is None else event .name == "key_press_event"
30473095
30483096 def init_layoutgrid (self ):
30493097 """Initialize the layoutgrid for use in constrained_layout."""
0 commit comments