Tkinter中的Python matplotlib动画

时间:2016-04-13 12:00:46

标签: python animation matplotlib tkinter

我按照教程使用matplotlib为简单的轨道旋转设置动画,但现在我想将它与Tkinter一起使用,显然我不能使用plt。

这是没有Tkinter的代码:

fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(6, 5.5)

ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
patch = plt.Circle((5, -5), 0.25, fc='y')

def init():
    patch.center = (5, 5)
    ax.add_patch(patch)
    return patch,

def animate(i):
    x, y = patch.center
    x = 5 + 3 * np.sin(np.radians(i))
    y = 5 + 3 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               frames=360, 
                               interval=20,
                               blit=False)

plt.show()

下面是在FigCanvasTkAgg中放置tkinter和动画的代码。 如何在没有plt的情况下制作轴和圆。 这就是我到目前为止所做的:

fig = Figure()
fig.set_dpi(100)
fig.set_size_inches(6, 5.5)

ax = axes(xlim=(0, 10), ylim=(0, 10)) # How to make ax and patch
patch = Circle((5, -5), 0.25, fc='y') # without plt

def init():
    patch.center = (5, 5)
    ax.add_patch(patch)
    return patch,

def animate(i):
    x, y = patch.center
    x = 5 + 3 * np.sin(np.radians(i))
    y = 5 + 3 * np.cos(np.radians(i))
    patch.center = (x, y)
    return patch,

root = Tk.Tk()

label = Tk.Label(root,text="Orbit Simulation").grid(column=0, row=0)

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(column=0,row=1)

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               frames=360, 
                               interval=20,
                               blit=False)

canvas.show()

Tk.mainloop()

0 个答案:

没有答案