嵌入一个matplotlib动画进入Tkinter的框架 [英] Embedding a matplotlib animation into a tkinter frame
问题描述
有关的一个项目,我在简谐运动模拟器(质量如何振荡一段时间内)工作。我有正确的,并且已经产生的数据有一个Tkinter的框架的工作中产生的图表。目前,它只能显示静态图,其中我的目的是要显示图形随时间的动画。
For a project I am working on a simple harmonic motion simulator (How a mass oscillates over time). I have got the data produced correctly and already have a graph produced within a tkinter frame work. At the moment it only shows a static graph where my objective is to display the graph as an animation over time.
所以,为了方便起见,我创建了一个模拟成使用下列code程序的:
So for ease sake I have created a mock up of the programme using the following code:
#---------Imports
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as Tk
from tkinter import ttk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#---------End of imports
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01) # x-array
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/10.0)) # update the data
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), interval=25, blit=False)
#plt.show() #What I want the object in tkinter to appear as
root = Tk.Tk()
label = ttk.Label(root,text="SHM Simulation").grid(column=0, row=0)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().grid(column=0,row=1)
Tk.mainloop()
这code将显示我想在Tkinter的框架的工作时plt.show()是未注释动画。我想能够把Tkinter的框架内的动画。
This code will display the animation that I want in the tkinter frame work when the plt.show() is uncommented. I would like to be able to place that animation within the framework of tkinter.
我也一直在matplotlib网站上,并认为所有的动画例子和他们都没有帮助。我也看着这个网站上的问题21179971,并且已经把pyplot图中的Tkinter的按钮,而我想放置的Tkinter框架内的身影。
I have also been on the matplotlib website and viewed all of the animation examples and none of them have helped. I have also looked on the question '21179971' on this website and that has placed the tkinter button within pyplot figure, whereas I would like to place the figure within a tkinter frame.
所以,我只想澄清,我希望能够把制作出来的动画时,#plt.show()中的Tkinter帧注释掉,即(根= TK())
So just to clarify, I would like to be able to place the animation produced when "#plt.show()" is uncommented in a tkinter frame, ie (root = tk())
推荐答案
我修改code:
#---------Imports
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#---------End of imports
fig = plt.Figure()
x = np.arange(0, 2*np.pi, 0.01) # x-array
def animate(i):
line.set_ydata(np.sin(x+i/10.0)) # update the data
return line,
root = Tk.Tk()
label = Tk.Label(root,text="SHM Simulation").grid(column=0, row=0)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(column=0,row=1)
ax = fig.add_subplot(111)
line, = ax.plot(x, np.sin(x))
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), interval=25, blit=False)
Tk.mainloop()
这篇关于嵌入一个matplotlib动画进入Tkinter的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!