3
3
from __future__ import division
4
4
5
5
import os , sys , math
6
+ import os .path
6
7
7
8
import Tkinter as Tk , FileDialog
8
- import tkagg # Paint image to Tk photo blitter extension
9
- from backend_agg import FigureCanvasAgg
10
9
11
- import os .path
10
+ # Paint image to Tk photo blitter extension
11
+ import import matplotlib .backends .tkagg as tkagg
12
+
13
+ from matplotlib .backends .backend_agg import FigureCanvasAgg
12
14
13
15
import matplotlib
14
16
from matplotlib .cbook import is_string_like
15
- from matplotlib .backend_bases import RendererBase , GraphicsContextBase , \
16
- FigureManagerBase , FigureCanvasBase , NavigationToolbar2 , cursors , TimerBase
17
+ from matplotlib .backend_bases import RendererBase , GraphicsContextBase
18
+ from matplotlib .backend_bases import FigureManagerBase , FigureCanvasBase ,
19
+ from matplotlib .backend_bases import NavigationToolbar2 , cursors , TimerBase
17
20
18
21
from matplotlib .figure import Figure
19
22
from matplotlib ._pylab_helpers import Gcf
@@ -61,24 +64,42 @@ def draw_if_interactive():
61
64
figManager .show ()
62
65
63
66
64
- def show ():
67
+ def show (block = False ):
65
68
"""
66
- Show all the figures and enter the gtk mainloop
69
+ Show all figures.
70
+
71
+ Temporary, experimental kwarg *block* defaults to False to
72
+ provide the behavior present throughout mpl history to date:
73
+ interactive mode is forced on, and show does not block.
67
74
68
- This should be the last line of your script. This function sets
69
- interactive mode to True, as detailed on
70
- http://matplotlib.sf.net/interactive.html
75
+ Set *block* to True to test the proposed new behavior,
76
+ consistent with other backends, in which show does not affect
77
+ interactive mode, and always blocks until all figures are closed.
78
+ In addition, the rcParam['tk.pythoninspect'] is ignored.
79
+
80
+ Use this kwarg only for testing; other backends do not accept
81
+ a kwarg to show, and might never do so.
71
82
"""
72
83
for manager in Gcf .get_all_fig_managers ():
73
84
manager .show ()
74
- import matplotlib
75
- matplotlib .interactive (True )
76
- if rcParams ['tk.pythoninspect' ]:
77
- os .environ ['PYTHONINSPECT' ] = '1'
78
- if show ._needmain :
85
+ if block :
86
+ # proposed new behavior; seems to make this backend consistent
87
+ # with others, with no drawbacks identified yet.
79
88
Tk .mainloop ()
80
- show ._needmain = False
81
- show ._needmain = True
89
+ else :
90
+ # long-time behavior: non-blocking, forces interactive mode
91
+ import matplotlib
92
+ matplotlib .interactive (True )
93
+ if rcParams ['tk.pythoninspect' ]:
94
+ os .environ ['PYTHONINSPECT' ] = '1'
95
+ if show ._needmain :
96
+ Tk .mainloop ()
97
+ show ._needmain = False
98
+
99
+ show ._needmain = True # This can go away if we eliminate block=False option.
100
+
101
+
102
+
82
103
83
104
def new_figure_manager (num , * args , ** kwargs ):
84
105
"""
@@ -98,7 +119,7 @@ def new_figure_manager(num, *args, **kwargs):
98
119
class TimerTk (TimerBase ):
99
120
'''
100
121
Subclass of :class:`backend_bases.TimerBase` that uses Tk's timer events.
101
-
122
+
102
123
Attributes:
103
124
* interval: The time between timer events in milliseconds. Default
104
125
is 1000 ms.
@@ -213,7 +234,7 @@ def __init__(self, figure, master=None, resize_callback=None):
213
234
root = self ._tkcanvas .winfo_toplevel ()
214
235
root .bind ("<MouseWheel>" , self .scroll_event_windows )
215
236
216
- # Can't get destroy events by binding ot _tkcanvas. Therefore, bind
237
+ # Can't get destroy events by binding to _tkcanvas. Therefore, bind
217
238
# to the window and filter.
218
239
def filter_destroy (evt ):
219
240
if evt .widget is self ._tkcanvas :
@@ -363,9 +384,9 @@ def new_timer(self, *args, **kwargs):
363
384
Creates a new backend-specific subclass of :class:`backend_bases.Timer`.
364
385
This is useful for getting periodic events through the backend's native
365
386
event loop. Implemented only for backends with GUIs.
366
-
387
+
367
388
optional arguments:
368
-
389
+
369
390
*interval*
370
391
Timer interval in milliseconds
371
392
*callbacks*
@@ -449,17 +470,15 @@ def show(self):
449
470
this function doesn't segfault but causes the
450
471
PyEval_RestoreThread: NULL state bug on win32
451
472
"""
452
-
453
- def destroy (* args ):
454
- self .window = None
455
- Gcf .destroy (self ._num )
456
-
457
- if not self ._shown : self .canvas ._tkcanvas .bind ("<Destroy>" , destroy )
458
473
_focus = windowing .FocusManager ()
459
474
if not self ._shown :
475
+ def destroy (* args ):
476
+ self .window = None
477
+ Gcf .destroy (self ._num )
478
+ self .canvas ._tkcanvas .bind ("<Destroy>" , destroy )
460
479
self .window .deiconify ()
461
480
# anim.py requires this
462
- if sys . platform == 'win32' : self .window .update ()
481
+ self .window .update ()
463
482
else :
464
483
self .canvas .draw_idle ()
465
484
self ._shown = True
0 commit comments