Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1e925f

Browse files
committed
Add private API retrieving the current event loop and backend GUI info.
Work towards implementation of backend switching.Note that the API is kept private for now as the lack of extensibilityis a bit unsatisfying; perhaps we'll figure out a better way to do itif other custom implementers (e.g., code editors?) get interested inlooking into it.The `required_event_loop` variable could be made private for now, ornot...
1 parent65158f9 commitf1e925f

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

‎lib/matplotlib/backends/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
importimportlib
22
importlogging
3+
importos
4+
importsys
35
importtraceback
46

57
importmatplotlib
@@ -14,6 +16,49 @@
1416
ifnotline.startswith(' File "<frozen importlib._bootstrap'))
1517

1618

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+
orsys.modules.get("PySide2.QtWidgets"))
30+
ifQtWidgetsandQtWidgets.QApplication.instance():
31+
return"qt5"
32+
QtGui= (sys.modules.get("PyQt4.QtGui")
33+
orsys.modules.get("PySide.QtGui"))
34+
ifQtGuiandQtGui.QApplication.instance():
35+
return"qt4"
36+
Gtk= (sys.modules.get("gi.repository.Gtk")
37+
orsys.modules.get("pgi.repository.Gtk"))
38+
ifGtkandGtk.main_level():
39+
return"gtk3"
40+
wx=sys.modules.get("wx")
41+
ifwxandwx.GetApp():
42+
return"wx"
43+
tkinter=sys.modules.get("tkinter")
44+
iftkinterandany(frame.f_code.co_filename==tkinter.__file__
45+
andframe.f_code.co_name=="mainloop"
46+
forframeinsys._current_frames().values()):
47+
return"tk"
48+
try:
49+
frommatplotlib.backendsimport_macosx
50+
exceptImportError:
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+
ifsys.platform.startswith("linux")andnotos.environ.get("DISPLAY"):
59+
return"headless"
60+
61+
1762
defpylab_setup(name=None):
1863
"""
1964
Return new_figure_manager, draw_if_interactive and show for pyplot.

‎lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ def trigger(self, *args):
994994

995995
@_Backend.export
996996
class_BackendTk(_Backend):
997+
required_event_loop="tk"
997998
FigureManager=FigureManagerTk
998999

9991000
@classmethod

‎lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ def error_msg_gtk(msg, parent=None):
971971

972972
@_Backend.export
973973
class_BackendGTK3(_Backend):
974+
required_event_loop="gtk3"
974975
FigureCanvas=FigureCanvasGTK3
975976
FigureManager=FigureManagerGTK3
976977

‎lib/matplotlib/backends/backend_qt4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
@_BackendQT5.export
99
class_BackendQT4(_BackendQT5):
10-
pass
10+
required_event_loop="qt4"

‎lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ def exception_handler(type, value, tb):
11091109

11101110
@_Backend.export
11111111
class_BackendQT5(_Backend):
1112+
required_event_loop="qt5"
11121113
FigureCanvas=FigureCanvasQT
11131114
FigureManager=FigureManagerQT
11141115

‎lib/matplotlib/backends/backend_wx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,7 @@ def OnPrintPage(self, page):
19301930

19311931
@_Backend.export
19321932
class_BackendWx(_Backend):
1933+
required_event_loop="wx"
19331934
FigureCanvas=FigureCanvasWx
19341935
FigureManager=FigureManagerWx
19351936
_frame_class=FigureFrameWx

‎src/_macosx.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,16 @@ - (int)index
27892789
}
27902790
@end
27912791

2792+
static PyObject*
2793+
event_loop_is_running(PyObject* self)
2794+
{
2795+
if ([NSAppisRunning]) {
2796+
Py_RETURN_TRUE;
2797+
}else {
2798+
Py_RETURN_FALSE;
2799+
}
2800+
}
2801+
27922802
static PyObject*
27932803
show(PyObject* self)
27942804
{
@@ -3038,6 +3048,11 @@ static bool verify_framework(void)
30383048
}
30393049

30403050
staticstruct PyMethodDef methods[] = {
3051+
{"event_loop_is_running",
3052+
(PyCFunction)event_loop_is_running,
3053+
METH_NOARGS,
3054+
"Return whether the NSApp main event loop is currently running."
3055+
},
30413056
{"show",
30423057
(PyCFunction)show,
30433058
METH_NOARGS,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp