我正在尝试在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的根窗口。当执行时,会执行打印“rotate button press…”行,但似乎其余部分没有执行。我需要以某种方式将图像句柄传递给旋转函数,或者返回旋转后的图像吗?
pyplot
。否则会出现两个 GUI 主循环的竞争问题。 - tacaswellpylot
,我该如何访问imshow()
? - not link