Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Tkinter gui中更新matplotlib imshow_Python_Matplotlib_Tkinter - Fatal编程技术网

Python 从Tkinter gui中更新matplotlib imshow

Python 从Tkinter gui中更新matplotlib imshow,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,我试图在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

我试图在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()
#最简单的示例。。。
导入matplotlib,sys
matplotlib.use('TkAgg')
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
从matplotlib导入pylab作为plt
从scipy导入ndimage
如果系统版本信息[0]<3:
将Tkinter作为Tk导入
其他:
将tkinter作为Tk导入
root=Tk.Tk()
root.wm_标题(“最小示例”)
image=plt.imread('test.jpg'))
图=零件图(图尺寸=(5,4))
im=plt.imshow(图像)#稍后使用a.set_数据(新数据)
ax=plt.gca()
ax.setxticklabels([])
ax.设置标签([])
#拉文加地区
canvas=FigureCanvasTkAgg(图,主控=根)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=1)
def旋转(*args):
打印“旋转按钮按下…”
θ=90
旋转=nImage.rotate(图像,θ)
im.set_数据(旋转)
root.update()
def退出(*参数):
打印“退出按钮按下…”
root.quit()
root.destroy()
按钮旋转=Tk.按钮(主控=根,文本=旋转,命令=旋转)
button\u quit=Tk.button(master=root,text=quit,command=quit)
按钮退出包装(侧面=左侧)
按钮旋转包()
Tk.mainloop()

如您所见,我使用imshow()加载了一个图像,然后尝试使用set_data()更新图像的数据,然后希望使用root.update()更新gui的根窗口。执行时,将执行打印“旋转按钮按下…”行,但其他行似乎不执行。我需要如何将图像句柄传递给旋转函数,还是返回旋转后的图像

画布需要重新绘制,请尝试以下操作:

def rotate(*args):
    print 'rotate button press...'
    theta = 90
    rotated = ndimage.rotate(image, theta)
    im.set_data(rotated)
    canvas.draw()
更新以保持旋转

注意这是将
图像
保存为
的属性。也许不是最好的方法,但它适用于示例

root = Tk.Tk()
root.wm_title("minimal example")

root.image = plt.imread('test.jpg')
fig = plt.figure(figsize=(5,4))
im = plt.imshow(root.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...'
    root.image = ndimage.rotate(root.image, 90)
    im.set_data(root.image)
    canvas.draw()

这似乎有效,但只有一次。如果我再按一下旋转按钮,它似乎什么也不起作用。(是否需要以某种方式保存图像的新状态?)。我将使用另一个修复程序进行更新,但您最好进行重构,生成自己的Tkinter.Frame子类,以便保存应用程序的属性…但这是另一个问题。在嵌入时不应导入
pyplot
。如果要嵌入,请不要导入
pyplot
。您最终会遇到gui主循环的决斗,这是有道理的,但是如果我没有使用
pylot
,如何访问
imshow()
?请参阅