Matplotlib vs tkinter event handling

Hi all,

I’m trying to understand how event handling works in matplotlib. Using the docs to try and form a mental model of how it works is testing to say the least. I think I’ve ascertained the following things, but if you’re reading this and don’t understand it yourself, remember everything I think I know may be false:

  • either a matplotlib event loop, or an event loop native to one of the supported GUI toolkits (e.g tkinter) can be used
  • for simple plots that the programs author does not embed in a larger graphical application, a matplotlib event loop is used
  • when embedding matplotlib in a larger graphical application, either type of event loop can be used, but a matplotlib event loop will supply callback routines with information more relevant to the task at hand (the event’s data coordinates, etc.)
  • the event names used with the matplotlib callback registering API matplotlib.backend_bases.FigureCanvasBase.mpl_connect() are taken from Gtk, so will differ from the name of the equivalent event received from the GUI tookit if a different - i.e not Gtk - backend is used (‘mouse_button’ vs tkinter’s ‘<ButtonPress>’, for example )

What makes me doubt all this is that in tkinter at least, there are many default event bindings to handle the most common events, so for the matplotlib event loop to work, it would also have to implement those default bindings, no? and it would also have to perform idle tasks like redrawing the application window?

Very confusing! any help would be much appreciated :slightly_smiling_face:

Edit: looking at the embedding in Tk example, that code calls .mpl_connect(), binding a callback to 'key_press_event', but then runs the tkinter event loop without ever doing anything to explicitly start or stop a matplotlib event loop, which doesn’t agree with my current mental model, hmm…