我正在使用以下教程点的代码。在这里,退出按钮停止程序,但我想暂停动画在另一个按钮按下。我在图中找到了使用onclick()的其他资源,但我想通过Pause按钮来实现这一点。
我怎样才能实现这一点?
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib import pyplot as plt, animation
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
plt.axes(xlim=(0, 2), ylim=(-2, 2))
fig = plt.Figure(dpi=100)
ax = fig.add_subplot(xlim=(0, 2), ylim=(-1, 1))
line, = ax.plot([], [], lw=2)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()
canvas.mpl_connect(
"key_press_event", lambda event: print(f"you pressed {event.key}"))
canvas.mpl_connect("key_press_event", key_press_handler)
button = tkinter.Button(master=root, text="Quit", command=root.quit)
button.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)
tkinter.mainloop()
发布于 2021-11-18 22:04:50
我的答案是基于答案here,它有一个很好的解释使用点击暂停动画(这你已经研究过了)。我的答案的功能与示例相同,只是修改由tkinter按钮驱动的暂停函数,而不是鼠标单击,并在代码中实现它。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import tkinter as tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
plt.axes(xlim=(0, 2), ylim=(-1, 1))
fig = plt.Figure(dpi=100)
ax = fig.add_subplot(xlim=(0, 2), ylim=(-1, 1))
line, = ax.plot([], [], lw=2)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()
canvas.mpl_connect(
"key_press_event", lambda event: print(f"you pressed {event.key}"))
canvas.mpl_connect("key_press_event", key_press_handler)
button = tkinter.Button(master=root, text="Quit", command=root.quit)
button.pack(side=tkinter.BOTTOM)
# Pause button and function
pause = False
def pause_animation():
global pause
pause ^= True
button2 = tkinter.Button(master=root, text="Pause", command=pause_animation)
button2.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def animation_data():
tMax = 100 # 0.01*tMax/dt needs to be an int for smooth animation
dt = 1 # Changing dt changes the "speed" that the curve moves on the plot
y = 0.0
t = 0
x = np.linspace(0, 2, 1000)
while t<tMax: # This is included to reset t so it doesn't get too large for long runs
if not pause:
y = np.sin(2 * np.pi * (x - 0.01 *t))
t = t + dt
yield x, y
def animate(animation_data):
x, y = animation_data[0], animation_data[1]
line.set_data(x, y)
return line,
ani = animation.FuncAnimation(fig, animate, animation_data, blit=True, interval=10,
repeat=True)
tkinter.mainloop()
https://stackoverflow.com/questions/70025615
复制相似问题