2929 raise ImportError from e
3030
3131from gi .repository import Gio , GLib , GObject , Gtk , Gdk
32+ from ._backend_gtk import (
33+ _create_application , _shutdown_application ,
34+ backend_version , _BackendGTK )
3235
3336
3437_log = logging .getLogger (__name__ )
3538
36- backend_version = "%s.%s.%s" % (
37- Gtk .get_major_version (), Gtk .get_minor_version (), Gtk .get_micro_version ())
38-
3939
4040@_api .caching_module_getattr # module-level deprecations
4141class __getattr__ :
@@ -56,46 +56,6 @@ def cursord(self):
5656 return {}
5757
5858
59- # Placeholder
60- _application = None
61-
62-
63- def _shutdown_application (app ):
64- # The application might prematurely shut down if Ctrl-C'd out of IPython,
65- # so close all windows.
66- for win in app .get_windows ():
67- win .destroy ()
68- # The PyGObject wrapper incorrectly thinks that None is not allowed, or we
69- # would call this:
70- # Gio.Application.set_default(None)
71- # Instead, we set this property and ignore default applications with it:
72- app ._created_by_matplotlib = True
73- global _application
74- _application = None
75-
76-
77- def _create_application ():
78- global _application
79-
80- if _application is None :
81- app = Gio .Application .get_default ()
82- if app is None or getattr (app , '_created_by_matplotlib' ):
83- # display_is_valid returns False only if on Linux and neither X11
84- # nor Wayland display can be opened.
85- if not mpl ._c_internal_utils .display_is_valid ():
86- raise RuntimeError ('Invalid DISPLAY variable' )
87- _application = Gtk .Application .new ('org.matplotlib.Matplotlib3' ,
88- Gio .ApplicationFlags .NON_UNIQUE )
89- # The activate signal must be connected, but we don't care for
90- # handling it, since we don't do any remote processing.
91- _application .connect ('activate' , lambda * args , ** kwargs : None )
92- _application .connect ('shutdown' , _shutdown_application )
93- _application .register ()
94- cbook ._setup_new_guiapp ()
95- else :
96- _application = app
97-
98-
9959@functools .lru_cache ()
10060def _mpl_to_gtk_cursor (mpl_cursor ):
10161 name = _api .check_getitem ({
@@ -373,9 +333,9 @@ class FigureManagerGTK3(FigureManagerBase):
373333
374334 """
375335 def __init__ (self , canvas , num ):
376- _create_application ()
336+ app = _create_application ()
377337 self .window = Gtk .Window ()
378- _application .add_window (self .window )
338+ app .add_window (self .window )
379339 super ().__init__ (canvas , num )
380340
381341 self .window .set_wmclass ("matplotlib" , "Matplotlib" )
@@ -868,18 +828,6 @@ def error_msg_gtk(msg, parent=None):
868828
869829
870830@_Backend .export
871- class _BackendGTK3 (_Backend ):
831+ class _BackendGTK3 (_BackendGTK ):
872832 FigureCanvas = FigureCanvasGTK3
873833 FigureManager = FigureManagerGTK3
874-
875- @staticmethod
876- def mainloop ():
877- global _application
878- if _application is None :
879- return
880-
881- try :
882- _application .run () # Quits when all added windows close.
883- finally :
884- # Running after quit is undefined, so create a new one next time.
885- _application = None
0 commit comments