_blocking_input.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. def blocking_input_loop(figure, event_names, timeout, handler):
  2. """
  3. Run *figure*'s event loop while listening to interactive events.
  4. The events listed in *event_names* are passed to *handler*.
  5. This function is used to implement `.Figure.waitforbuttonpress`,
  6. `.Figure.ginput`, and `.Axes.clabel`.
  7. Parameters
  8. ----------
  9. figure : `~matplotlib.figure.Figure`
  10. event_names : list of str
  11. The names of the events passed to *handler*.
  12. timeout : float
  13. If positive, the event loop is stopped after *timeout* seconds.
  14. handler : Callable[[Event], Any]
  15. Function called for each event; it can force an early exit of the event
  16. loop by calling ``canvas.stop_event_loop()``.
  17. """
  18. if figure.canvas.manager:
  19. figure.show() # Ensure that the figure is shown if we are managing it.
  20. # Connect the events to the on_event function call.
  21. cids = [figure.canvas.mpl_connect(name, handler) for name in event_names]
  22. try:
  23. figure.canvas.start_event_loop(timeout) # Start event loop.
  24. finally: # Run even on exception like ctrl-c.
  25. # Disconnect the callbacks.
  26. for cid in cids:
  27. figure.canvas.mpl_disconnect(cid)