__init__.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import importlib
  2. import logging
  3. import os
  4. import sys
  5. import matplotlib
  6. from matplotlib import cbook
  7. from matplotlib.backend_bases import _Backend
  8. _log = logging.getLogger(__name__)
  9. # NOTE: plt.switch_backend() (called at import time) will add a "backend"
  10. # attribute here for backcompat.
  11. def _get_running_interactive_framework():
  12. """
  13. Return the interactive framework whose event loop is currently running, if
  14. any, or "headless" if no event loop can be started, or None.
  15. Returns
  16. -------
  17. Optional[str]
  18. One of the following values: "qt5", "qt4", "gtk3", "wx", "tk",
  19. "macosx", "headless", ``None``.
  20. """
  21. QtWidgets = (sys.modules.get("PyQt5.QtWidgets")
  22. or sys.modules.get("PySide2.QtWidgets"))
  23. if QtWidgets and QtWidgets.QApplication.instance():
  24. return "qt5"
  25. QtGui = (sys.modules.get("PyQt4.QtGui")
  26. or sys.modules.get("PySide.QtGui"))
  27. if QtGui and QtGui.QApplication.instance():
  28. return "qt4"
  29. Gtk = sys.modules.get("gi.repository.Gtk")
  30. if Gtk and Gtk.main_level():
  31. return "gtk3"
  32. wx = sys.modules.get("wx")
  33. if wx and wx.GetApp():
  34. return "wx"
  35. tkinter = sys.modules.get("tkinter")
  36. if tkinter:
  37. for frame in sys._current_frames().values():
  38. while frame:
  39. if frame.f_code == tkinter.mainloop.__code__:
  40. return "tk"
  41. frame = frame.f_back
  42. if 'matplotlib.backends._macosx' in sys.modules:
  43. if sys.modules["matplotlib.backends._macosx"].event_loop_is_running():
  44. return "macosx"
  45. if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
  46. return "headless"
  47. return None