Tkinter GUI 内の matplotlib imshow ウィンドウのデータを更新しようとしています。コードの例は次のとおりです。
#minimal example...
import matplotlib, sys
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pylab as plt
from scipy import ndimage
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.wm_title("minimal example")
image = plt.imread('test.jpg')
fig = plt.figure(figsize=(5,4))
im = plt.imshow(image) # later use a.set_data(new_data)
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def rotate(*args):
print 'rotate button press...'
theta = 90
rotated = ndimage.rotate(image, theta)
im.set_data(rotated)
root.update()
def quit(*args):
print 'quit button press...'
root.quit()
root.destroy()
button_rotate = Tk.Button(master = root, text = 'Rotate', command = rotate)
button_quit = Tk.Button(master = root, text = 'Quit', command = quit)
button_quit.pack(side=Tk.LEFT)
button_rotate.pack()
Tk.mainloop()
ご覧のとおり、imshow() を使用して画像をロードし、set_data() を使用して画像のデータを更新してから、root.update() を使用して GUI のルート ウィンドウを更新します。実行すると、print 'rotate button press...' 行が実行されますが、残りは実行されないようです。何らかの方法で画像ハンドルを回転関数に渡すか、回転した画像を返す必要がありますか?