|
1 | 1 | import importlib
|
2 | 2 | import logging
|
| 3 | +import os |
| 4 | +import sys |
3 | 5 | import traceback
|
4 | 6 |
|
5 | 7 | import matplotlib
|
|
14 | 16 | if not line.startswith(' File "<frozen importlib._bootstrap'))
|
15 | 17 |
|
16 | 18 |
|
| 19 | +def _get_current_event_loop(): |
| 20 | + """Return the currently running event loop if any, or "headless", or None. |
| 21 | + "headless" indicates that no event loop can be started. |
| 22 | + Returns |
| 23 | + ------- |
| 24 | + Optional[str] |
| 25 | + One of the following values: "qt5", "qt4", "gtk3", "wx", "tk", |
| 26 | + "macosx", "headless", ``None``. |
| 27 | + """ |
| 28 | + QtWidgets = (sys.modules.get("PyQt5.QtWidgets") |
| 29 | + or sys.modules.get("PySide2.QtWidgets")) |
| 30 | + if QtWidgets and QtWidgets.QApplication.instance(): |
| 31 | + return "qt5" |
| 32 | + QtGui = (sys.modules.get("PyQt4.QtGui") |
| 33 | + or sys.modules.get("PySide.QtGui")) |
| 34 | + if QtGui and QtGui.QApplication.instance(): |
| 35 | + return "qt4" |
| 36 | + Gtk = (sys.modules.get("gi.repository.Gtk") |
| 37 | + or sys.modules.get("pgi.repository.Gtk")) |
| 38 | + if Gtk and Gtk.main_level(): |
| 39 | + return "gtk3" |
| 40 | + wx = sys.modules.get("wx") |
| 41 | + if wx and wx.GetApp(): |
| 42 | + return "wx" |
| 43 | + tkinter = sys.modules.get("tkinter") |
| 44 | + if tkinter and any(frame.f_code.co_filename == tkinter.__file__ |
| 45 | + and frame.f_code.co_name == "mainloop" |
| 46 | + for frame in sys._current_frames().values()): |
| 47 | + return "tk" |
| 48 | + try: |
| 49 | + from matplotlib.backends import _macosx |
| 50 | + except ImportError: |
| 51 | + pass |
| 52 | + else: |
| 53 | + # Note that the NSApp event loop is also running when a non-native |
| 54 | + # toolkit (e.g. Qt5) is active, but in that case we want to report the |
| 55 | + # other toolkit; thus, this check comes after the other toolkits. |
| 56 | + if _macosx.event_loop_is_running(): |
| 57 | + return "macosx" |
| 58 | + if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"): |
| 59 | + return "headless" |
| 60 | + |
| 61 | + |
17 | 62 | def pylab_setup(name=None):
|
18 | 63 | """
|
19 | 64 | Return new_figure_manager, draw_if_interactive and show for pyplot.
|
|
0 commit comments