Skip to content

Commit 032f40b

Browse files
committed
backend_tkagg: show() gets an experimental kwarg for testing blocking mode.
By default, the behavior of show() is unchanged--it does not block, and it forces interactive mode on. show(block=True) is intended to make it behave the same as in the other common backends, blocking until all figures have been closed. svn path=/trunk/matplotlib/; revision=8420
1 parent f44627f commit 032f40b

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
from __future__ import division
44

55
import os, sys, math
6+
import os.path
67

78
import Tkinter as Tk, FileDialog
8-
import tkagg # Paint image to Tk photo blitter extension
9-
from backend_agg import FigureCanvasAgg
109

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
1214

1315
import matplotlib
1416
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
1720

1821
from matplotlib.figure import Figure
1922
from matplotlib._pylab_helpers import Gcf
@@ -61,24 +64,42 @@ def draw_if_interactive():
6164
figManager.show()
6265

6366

64-
def show():
67+
def show(block=False):
6568
"""
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.
6774
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.
7182
"""
7283
for manager in Gcf.get_all_fig_managers():
7384
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.
7988
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+
82103

83104
def new_figure_manager(num, *args, **kwargs):
84105
"""
@@ -98,7 +119,7 @@ def new_figure_manager(num, *args, **kwargs):
98119
class TimerTk(TimerBase):
99120
'''
100121
Subclass of :class:`backend_bases.TimerBase` that uses Tk's timer events.
101-
122+
102123
Attributes:
103124
* interval: The time between timer events in milliseconds. Default
104125
is 1000 ms.
@@ -213,7 +234,7 @@ def __init__(self, figure, master=None, resize_callback=None):
213234
root = self._tkcanvas.winfo_toplevel()
214235
root.bind("<MouseWheel>", self.scroll_event_windows)
215236

216-
# Can't get destroy events by binding ot _tkcanvas. Therefore, bind
237+
# Can't get destroy events by binding to _tkcanvas. Therefore, bind
217238
# to the window and filter.
218239
def filter_destroy(evt):
219240
if evt.widget is self._tkcanvas:
@@ -363,9 +384,9 @@ def new_timer(self, *args, **kwargs):
363384
Creates a new backend-specific subclass of :class:`backend_bases.Timer`.
364385
This is useful for getting periodic events through the backend's native
365386
event loop. Implemented only for backends with GUIs.
366-
387+
367388
optional arguments:
368-
389+
369390
*interval*
370391
Timer interval in milliseconds
371392
*callbacks*
@@ -449,17 +470,15 @@ def show(self):
449470
this function doesn't segfault but causes the
450471
PyEval_RestoreThread: NULL state bug on win32
451472
"""
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)
458473
_focus = windowing.FocusManager()
459474
if not self._shown:
475+
def destroy(*args):
476+
self.window = None
477+
Gcf.destroy(self._num)
478+
self.canvas._tkcanvas.bind("<Destroy>", destroy)
460479
self.window.deiconify()
461480
# anim.py requires this
462-
if sys.platform=='win32' : self.window.update()
481+
self.window.update()
463482
else:
464483
self.canvas.draw_idle()
465484
self._shown = True

0 commit comments

Comments
 (0)