Skip to content

Commit 4f89ef2

Browse files
authored
Merge pull request #9275 from anntzer/tkagg-anim
Tkagg fixes
2 parents 874116f + 2597f3d commit 4f89ef2

File tree

4 files changed

+34
-90
lines changed

4 files changed

+34
-90
lines changed

examples/animation/simple_anim.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def animate(i):
19-
line.set_ydata(np.sin(x + i/10.0)) # update the data
19+
line.set_ydata(np.sin(x + i / 100)) # update the data
2020
return line,
2121

2222

@@ -25,6 +25,6 @@ def init():
2525
line.set_ydata(np.ma.array(x, mask=True))
2626
return line,
2727

28-
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
29-
interval=25, blit=True)
28+
ani = animation.FuncAnimation(
29+
fig, animate, init_func=init, interval=2, blit=True)
3030
plt.show()

examples/user_interfaces/embedding_in_tk2_sgskip.py

-49
This file was deleted.

examples/user_interfaces/embedding_in_tk_sgskip.py

+21-30
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,51 @@
44
===============
55
66
"""
7-
import matplotlib
8-
matplotlib.use('TkAgg')
9-
10-
from numpy import arange, sin, pi
11-
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
12-
# implement the default mpl key bindings
13-
from matplotlib.backend_bases import key_press_handler
147

8+
from six.moves import tkinter as Tk
159

10+
from matplotlib.backends.backend_tkagg import (
11+
FigureCanvasTkAgg, NavigationToolbar2TkAgg)
12+
# Implement the default Matplotlib key bindings.
13+
from matplotlib.backend_bases import key_press_handler
1614
from matplotlib.figure import Figure
1715

18-
import sys
19-
if sys.version_info[0] < 3:
20-
import Tkinter as Tk
21-
else:
22-
import tkinter as Tk
23-
24-
root = Tk.Tk()
25-
root.wm_title("Embedding in TK")
16+
import numpy as np
2617

2718

28-
f = Figure(figsize=(5, 4), dpi=100)
29-
a = f.add_subplot(111)
30-
t = arange(0.0, 3.0, 0.01)
31-
s = sin(2*pi*t)
32-
33-
a.plot(t, s)
19+
root = Tk.Tk()
20+
root.wm_title("Embedding in Tk")
3421

22+
fig = Figure(figsize=(5, 4), dpi=100)
23+
t = np.arange(0, 3, .01)
24+
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
3525

36-
# a tk.DrawingArea
37-
canvas = FigureCanvasTkAgg(f, master=root)
38-
canvas.show()
26+
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
27+
canvas.draw()
3928
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
4029

4130
toolbar = NavigationToolbar2TkAgg(canvas, root)
4231
toolbar.update()
4332
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
4433

4534

46-
def on_key_event(event):
47-
print('you pressed %s' % event.key)
35+
def on_key_press(event):
36+
print("you pressed {}".format(event.key))
4837
key_press_handler(event, canvas, toolbar)
4938

50-
canvas.mpl_connect('key_press_event', on_key_event)
39+
40+
canvas.mpl_connect("key_press_event", on_key_press)
5141

5242

5343
def _quit():
5444
root.quit() # stops mainloop
5545
root.destroy() # this is necessary on Windows to prevent
5646
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
5747

58-
button = Tk.Button(master=root, text='Quit', command=_quit)
48+
49+
button = Tk.Button(master=root, text="Quit", command=_quit)
5950
button.pack(side=Tk.BOTTOM)
6051

6152
Tk.mainloop()
62-
# If you put root.destroy() here, it will cause an error if
63-
# the window is closed with the window manager.
53+
# If you put root.destroy() here, it will cause an error if the window is
54+
# closed with the window manager.

lib/matplotlib/backends/backend_tkagg.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def resize(self, event):
225225
master=self._tkcanvas, width=int(width), height=int(height))
226226
self._tkcanvas.create_image(int(width/2),int(height/2),image=self._tkphoto)
227227
self.resize_event()
228-
self.show()
228+
self.draw()
229229

230230
# a resizing will in general move the pointer position
231231
# relative to the canvas, so process it as a motion notify
@@ -301,10 +301,12 @@ def draw(self):
301301
self._master.update_idletasks()
302302

303303
def blit(self, bbox=None):
304-
tkagg.blit(self._tkphoto, self.renderer._renderer, bbox=bbox, colormode=2)
304+
tkagg.blit(
305+
self._tkphoto, self.renderer._renderer, bbox=bbox, colormode=2)
305306
self._master.update_idletasks()
306307

307-
show = draw
308+
show = cbook.deprecated("2.2", name="FigureCanvasTkAgg.show",
309+
alternative="FigureCanvasTkAgg.draw")(draw)
308310

309311
def draw_idle(self):
310312
'update drawing area only if idle'
@@ -530,6 +532,8 @@ def resize(self, width, height=None):
530532

531533
# when a single parameter is given, consider it as a event
532534
if height is None:
535+
cbook.warn_deprecated("2.2", "FigureManagerTkAgg.resize now takes "
536+
"width and height as separate arguments")
533537
width = width.width
534538
else:
535539
self.canvas._tkcanvas.master.geometry("%dx%d" % (width, height))
@@ -549,8 +553,6 @@ def destroy(*args):
549553
Gcf.destroy(self._num)
550554
self.canvas._tkcanvas.bind("<Destroy>", destroy)
551555
self.window.deiconify()
552-
# anim.py requires this
553-
self.window.update()
554556
else:
555557
self.canvas.draw_idle()
556558
# Raise the new window.
@@ -732,8 +734,8 @@ def configure_subplots(self):
732734
window = Tk.Toplevel()
733735
canvas = FigureCanvasTkAgg(toolfig, master=window)
734736
toolfig.subplots_adjust(top=0.9)
735-
canvas.tool = SubplotTool(self.canvas.figure, toolfig)
736-
canvas.show()
737+
canvas.tool = SubplotTool(self.canvas.figure, toolfig)
738+
canvas.draw()
737739
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
738740
window.grab_set()
739741

@@ -1022,7 +1024,7 @@ def init_window(self):
10221024
canvas = FigureCanvasTkAgg(toolfig, master=self.window)
10231025
toolfig.subplots_adjust(top=0.9)
10241026
_tool = SubplotTool(self.figure, toolfig)
1025-
canvas.show()
1027+
canvas.draw()
10261028
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
10271029
self.window.protocol("WM_DELETE_WINDOW", self.destroy)
10281030

0 commit comments

Comments
 (0)