11"""
2- This provides several classes used for blocking interaction with figure windows:
2+ This provides several classes used for blocking interaction with figure
3+ windows:
34
45:class:`BlockingInput`
5- creates a callable object to retrieve events in a blocking way for interactive sessions
6+ creates a callable object to retrieve events in a blocking way for
7+ interactive sessions
68
79:class:`BlockingKeyMouseInput`
8- creates a callable object to retrieve key or mouse clicks in a blocking way for interactive sessions.
10+ creates a callable object to retrieve key or mouse clicks in a blocking
11+ way for interactive sessions.
912 Note: Subclass of BlockingInput. Used by waitforbuttonpress
1013
1114:class:`BlockingMouseInput`
12- creates a callable object to retrieve mouse clicks in a blocking way for interactive sessions.
15+ creates a callable object to retrieve mouse clicks in a blocking way for
16+ interactive sessions.
1317 Note: Subclass of BlockingInput. Used by ginput
1418
1519:class:`BlockingContourLabeler`
16- creates a callable object to retrieve mouse clicks in a blocking way that will then be used to place labels on a ContourSet
20+
21+ creates a callable object to retrieve mouse clicks in a blocking way that
22+ will then be used to place labels on a ContourSet
1723 Note: Subclass of BlockingMouseInput. Used by clabel
1824"""
1925
2228from matplotlib .cbook import is_sequence_of_strings
2329import matplotlib .lines as mlines
2430
31+
2532class BlockingInput (object ):
2633 """
2734 Class that creates a callable object to retrieve events in a
2835 blocking way.
2936 """
3037 def __init__ (self , fig , eventslist = ()):
3138 self .fig = fig
32- assert is_sequence_of_strings (eventslist ), "Requires a sequence of event name strings"
39+ assert is_sequence_of_strings (
40+ eventslist ), "Requires a sequence of event name strings"
3341 self .eventslist = eventslist
3442
3543 def on_event (self , event ):
@@ -60,13 +68,13 @@ def cleanup(self):
6068 for cb in self .callbacks :
6169 self .fig .canvas .mpl_disconnect (cb )
6270
63- self .callbacks = []
71+ self .callbacks = []
6472
65- def add_event (self ,event ):
73+ def add_event (self , event ):
6674 """For base class, this just appends an event to events."""
6775 self .events .append (event )
6876
69- def pop_event (self ,index = - 1 ):
77+ def pop_event (self , index = - 1 ):
7078 """
7179 This removes an event from the event list. Defaults to
7280 removing last event, but an index can be supplied. Note that
@@ -76,11 +84,11 @@ def pop_event(self,index=-1):
7684 """
7785 self .events .pop (index )
7886
79- def pop (self ,index = - 1 ):
87+ def pop (self , index = - 1 ):
8088 self .pop_event (index )
81- pop .__doc__ = pop_event .__doc__
89+ pop .__doc__ = pop_event .__doc__
8290
83- def __call__ (self , n = 1 , timeout = 30 ):
91+ def __call__ (self , n = 1 , timeout = 30 ):
8492 """
8593 Blocking call to retrieve n events
8694 """
@@ -96,18 +104,20 @@ def __call__(self, n=1, timeout=30 ):
96104
97105 # connect the events to the on_event function call
98106 for n in self .eventslist :
99- self .callbacks .append ( self .fig .canvas .mpl_connect (n , self .on_event ) )
107+ self .callbacks .append (
108+ self .fig .canvas .mpl_connect (n , self .on_event ))
100109
101110 try :
102111 # Start event loop
103112 self .fig .canvas .start_event_loop (timeout = timeout )
104- finally : # Run even on exception like ctrl-c
113+ finally : # Run even on exception like ctrl-c
105114 # Disconnect the callbacks
106115 self .cleanup ()
107116
108117 # Return the events in this case
109118 return self .events
110119
120+
111121class BlockingMouseInput (BlockingInput ):
112122 """
113123 Class that creates a callable object to retrieve mouse clicks in a
@@ -118,25 +128,23 @@ class BlockingMouseInput(BlockingInput):
118128 enter is like mouse button 2 and all others are like mouse button 1).
119129 """
120130
121- button_add = 1
122- button_pop = 3
123- button_stop = 2
131+ button_add = 1
132+ button_pop = 3
133+ button_stop = 2
124134
125135 def __init__ (self , fig , mouse_add = 1 , mouse_pop = 3 , mouse_stop = 2 ):
126136 BlockingInput .__init__ (self , fig = fig ,
127137 eventslist = ('button_press_event' ,
128- 'key_press_event' ) )
138+ 'key_press_event' ))
129139 self .button_add = mouse_add
130140 self .button_pop = mouse_pop
131- self .button_stop = mouse_stop
132-
133-
141+ self .button_stop = mouse_stop
134142
135143 def post_event (self ):
136144 """
137145 This will be called to process events
138146 """
139- assert len (self .events )> 0 , "No events yet"
147+ assert len (self .events ) > 0 , "No events yet"
140148
141149 if self .events [- 1 ].name == 'key_press_event' :
142150 self .key_event ()
@@ -171,56 +179,57 @@ def key_event(self):
171179
172180 if key in ['backspace' , 'delete' ]:
173181 self .mouse_event_pop (event )
174- elif key in ['escape' , 'enter' ]: # on windows XP and wxAgg, the enter key doesn't seem to register
182+ elif key in ['escape' , 'enter' ]:
183+ # on windows XP and wxAgg, the enter key doesn't seem to register
175184 self .mouse_event_stop (event )
176185 else :
177186 self .mouse_event_add (event )
178187
179- def mouse_event_add ( self , event ):
188+ def mouse_event_add (self , event ):
180189 """
181190 Will be called for any event involving a button other than
182191 button 2 or 3. This will add a click if it is inside axes.
183192 """
184193 if event .inaxes :
185194 self .add_click (event )
186- else : # If not a valid click, remove from event list
187- BlockingInput .pop (self ,- 1 )
195+ else : # If not a valid click, remove from event list
196+ BlockingInput .pop (self , - 1 )
188197
189- def mouse_event_stop ( self , event ):
198+ def mouse_event_stop (self , event ):
190199 """
191200 Will be called for any event involving button 2.
192201 Button 2 ends blocking input.
193202 """
194203
195204 # Remove last event just for cleanliness
196- BlockingInput .pop (self ,- 1 )
205+ BlockingInput .pop (self , - 1 )
197206
198207 # This will exit even if not in infinite mode. This is
199208 # consistent with MATLAB and sometimes quite useful, but will
200209 # require the user to test how many points were actually
201210 # returned before using data.
202211 self .fig .canvas .stop_event_loop ()
203212
204- def mouse_event_pop ( self , event ):
213+ def mouse_event_pop (self , event ):
205214 """
206215 Will be called for any event involving button 3.
207216 Button 3 removes the last click.
208217 """
209218 # Remove this last event
210- BlockingInput .pop (self ,- 1 )
219+ BlockingInput .pop (self , - 1 )
211220
212221 # Now remove any existing clicks if possible
213- if len (self .events )> 0 :
214- self .pop (event ,- 1 )
222+ if len (self .events ) > 0 :
223+ self .pop (event , - 1 )
215224
216- def add_click (self ,event ):
225+ def add_click (self , event ):
217226 """
218227 This add the coordinates of an event to the list of clicks
219228 """
220- self .clicks .append ((event .xdata ,event .ydata ))
229+ self .clicks .append ((event .xdata , event .ydata ))
221230
222231 verbose .report ("input %i: %f,%f" %
223- (len (self .clicks ),event .xdata , event .ydata ))
232+ (len (self .clicks ), event .xdata , event .ydata ))
224233
225234 # If desired plot up click
226235 if self .show_clicks :
@@ -230,9 +239,7 @@ def add_click(self,event):
230239 self .marks .append (line )
231240 self .fig .canvas .draw ()
232241
233-
234-
235- def pop_click (self ,event ,index = - 1 ):
242+ def pop_click (self , event , index = - 1 ):
236243 """
237244 This removes a click from the list of clicks. Defaults to
238245 removing the last click.
@@ -249,17 +256,16 @@ def pop_click(self,event,index=-1):
249256 # for the keyboard backspace event on windows XP wxAgg.
250257 # maybe event.inaxes here is a COPY of the actual axes?
251258
252-
253- def pop (self ,event ,index = - 1 ):
259+ def pop (self , event , index = - 1 ):
254260 """
255261 This removes a click and the associated event from the object.
256262 Defaults to removing the last click, but any index can be
257263 supplied.
258264 """
259- self .pop_click (event ,index )
260- BlockingInput .pop (self ,index )
265+ self .pop_click (event , index )
266+ BlockingInput .pop (self , index )
261267
262- def cleanup (self ,event = None ):
268+ def cleanup (self , event = None ):
263269 # clean the figure
264270 if self .show_clicks :
265271
@@ -278,28 +284,29 @@ def __call__(self, n=1, timeout=30, show_clicks=True):
278284 clicks.
279285 """
280286 self .show_clicks = show_clicks
281- self .clicks = []
282- self .marks = []
283- BlockingInput .__call__ (self ,n = n ,timeout = timeout )
287+ self .clicks = []
288+ self .marks = []
289+ BlockingInput .__call__ (self , n = n , timeout = timeout )
284290
285291 return self .clicks
286292
287- class BlockingContourLabeler ( BlockingMouseInput ):
293+
294+ class BlockingContourLabeler (BlockingMouseInput ):
288295 """
289296 Class that creates a callable object that uses mouse clicks or key
290297 clicks on a figure window to place contour labels.
291298 """
292- def __init__ (self ,cs ):
299+ def __init__ (self , cs ):
293300 self .cs = cs
294- BlockingMouseInput .__init__ (self , fig = cs .ax .figure )
301+ BlockingMouseInput .__init__ (self , fig = cs .ax .figure )
295302
296303 def add_click (self , event ):
297304 self .button1 (event )
298305
299306 def pop_click (self , event , index = - 1 ):
300307 self .button3 (event )
301308
302- def button1 (self ,event ):
309+ def button1 (self , event ):
303310 """
304311 This will be called if an event involving a button other than
305312 2 or 3 occcurs. This will add a label to a contour.
@@ -311,10 +318,10 @@ def button1(self,event):
311318 inline_spacing = self .inline_spacing ,
312319 transform = False )
313320 self .fig .canvas .draw ()
314- else : # Remove event if not valid
321+ else : # Remove event if not valid
315322 BlockingInput .pop (self )
316323
317- def button3 (self ,event ):
324+ def button3 (self , event ):
318325 """
319326 This will be called if button 3 is clicked. This will remove
320327 a label if not in inline mode. Unfortunately, if one is doing
@@ -329,26 +336,28 @@ def button3(self,event):
329336 self .cs .pop_label ()
330337 self .cs .ax .figure .canvas .draw ()
331338
332- def __call__ (self ,inline ,inline_spacing = 5 ,n = - 1 ,timeout = - 1 ):
333- self .inline = inline
334- self .inline_spacing = inline_spacing
339+ def __call__ (self , inline , inline_spacing = 5 , n = - 1 , timeout = - 1 ):
340+ self .inline = inline
341+ self .inline_spacing = inline_spacing
335342
336- BlockingMouseInput .__call__ (self ,n = n ,timeout = timeout ,
343+ BlockingMouseInput .__call__ (self , n = n , timeout = timeout ,
337344 show_clicks = False )
338345
346+
339347class BlockingKeyMouseInput (BlockingInput ):
340348 """
341349 Class that creates a callable object to retrieve a single mouse or
342350 keyboard click
343351 """
344352 def __init__ (self , fig ):
345- BlockingInput .__init__ (self , fig = fig , eventslist = ('button_press_event' ,'key_press_event' ) )
353+ BlockingInput .__init__ (self , fig = fig , eventslist = (
354+ 'button_press_event' , 'key_press_event' ))
346355
347356 def post_event (self ):
348357 """
349358 Determines if it is a key event
350359 """
351- assert len (self .events )> 0 , "No events yet"
360+ assert len (self .events ) > 0 , "No events yet"
352361
353362 self .keyormouse = self .events [- 1 ].name == 'key_press_event'
354363
@@ -358,7 +367,6 @@ def __call__(self, timeout=30):
358367 Returns True if key click, False if mouse, or None if timeout
359368 """
360369 self .keyormouse = None
361- BlockingInput .__call__ (self ,n = 1 ,timeout = timeout )
370+ BlockingInput .__call__ (self , n = 1 , timeout = timeout )
362371
363372 return self .keyormouse
364-
0 commit comments