26
26
import re
27
27
import sys
28
28
import time
29
+ try :
30
+ import threading
31
+ except ImportError :
32
+ import dummy_threading as threading
29
33
30
34
from cycler import cycler
31
35
import matplotlib
@@ -175,6 +179,11 @@ def findobj(o=None, match=None, include_self=True):
175
179
return o .findobj (match , include_self = include_self )
176
180
177
181
182
+ def _get_required_interactive_framework (backend_mod ):
183
+ return getattr (
184
+ backend_mod .FigureCanvas , "required_interactive_framework" , None )
185
+
186
+
178
187
def switch_backend (newbackend ):
179
188
"""
180
189
Close all open figures and set the Matplotlib backend.
@@ -217,8 +226,7 @@ def switch_backend(newbackend):
217
226
class backend_mod (matplotlib .backend_bases ._Backend ):
218
227
locals ().update (vars (importlib .import_module (backend_name )))
219
228
220
- required_framework = getattr (
221
- backend_mod .FigureCanvas , "required_interactive_framework" , None )
229
+ required_framework = _get_required_interactive_framework (backend_mod )
222
230
if required_framework is not None :
223
231
current_framework = cbook ._get_running_interactive_framework ()
224
232
if (current_framework and required_framework
@@ -242,8 +250,17 @@ class backend_mod(matplotlib.backend_bases._Backend):
242
250
matplotlib .backends .backend = newbackend
243
251
244
252
253
+ def _warn_if_gui_out_of_main_thread ():
254
+ if (_get_required_interactive_framework (_backend_mod )
255
+ and threading .current_thread () is not threading .main_thread ()):
256
+ cbook ._warn_external (
257
+ "Starting a Matplotlib GUI outside of the main thread will likely "
258
+ "fail." )
259
+
260
+
245
261
def new_figure_manager (* args , ** kwargs ):
246
262
"""Create a new figure manager instance."""
263
+ _warn_if_gui_out_of_main_thread ()
247
264
return _backend_mod .new_figure_manager (* args , ** kwargs )
248
265
249
266
@@ -270,6 +287,7 @@ def show(*args, **kwargs):
270
287
This is experimental, and may be set to ``True`` or ``False`` to
271
288
override the blocking behavior described above.
272
289
"""
290
+ _warn_if_gui_out_of_main_thread ()
273
291
return _backend_mod .show (* args , ** kwargs )
274
292
275
293
0 commit comments