How is the matplotlib event loop implemented?

Hi all, I’ve gone round in circles for days trying to get clarity on this; I want to know how the matplotlib event loop is implemented.

When a call is made to .FigureCanvasBase.mpl_connect(), what happens? Is the given callback routine bound to the given event in the GUI toolkit’s event loop, or is there a separate loop implemented in matplotlib.

if the former is the case, there must be more going on under the hood as callback routines registered with .mpl_connect() are passed an event object provisioned with data attributes more specific to matplotlib, so the GUI event loop would have to get that from somewhere.

And, if the latter is the case, how does the embedding in Tk example from the documentation make two calls to .mpl_connect() and then run the Tk .mainloop() without ever running any sort of alternative matplotlib event loop? This is more suggestive of the first theory.

Any help is much appreciated!

mpl_connect adds callbacks to the canvas CallbackRegistry. This is just a light dictionary-like holder of callbacks. Events are triggered by the various backends (which wrap GUI toolkits) on this object, by toolkit-specific handlers. For example, the Tk handlers are connected on the canvas here:

1 Like