From cf659c6f64a57d06b43eb50e6a6c138939127b3b Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 2 Nov 2021 22:52:34 -0400 Subject: [PATCH] Fix interrupting GTK on plain Python When the application is shut down, it's intended that all windows are closed. This shutdown event is also run if interrupted with Ctrl-C. But then the main loop is no longer running, and windows do not notice the close event sent to them. In IPython, the input hook runs the loop and allows the windows to close down. But in plain Python, there is no input hook integration, and the windows sit around in limbo without accepting any events. Absent any new input hook integrations, the workaround is to iterate the main loop on interrupt. --- lib/matplotlib/backends/_backend_gtk.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/matplotlib/backends/_backend_gtk.py b/lib/matplotlib/backends/_backend_gtk.py index 7b0341d644fb..2b095c3bc7a9 100644 --- a/lib/matplotlib/backends/_backend_gtk.py +++ b/lib/matplotlib/backends/_backend_gtk.py @@ -169,6 +169,13 @@ def mainloop(): try: _application.run() # Quits when all added windows close. + except KeyboardInterrupt: + # Ensure all windows can process their close event from + # _shutdown_application. + context = GLib.MainContext.default() + while context.pending(): + context.iteration(True) + raise finally: # Running after quit is undefined, so create a new one next time. _application = None