首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌入在tkinter中的按钮按下matplotlib的暂停/停止动画

嵌入在tkinter中的按钮按下matplotlib的暂停/停止动画
EN

Stack Overflow用户
提问于 2021-11-18 19:27:30
回答 1查看 486关注 0票数 0

我正在使用以下教程点的代码。在这里,退出按钮停止程序,但我想暂停动画在另一个按钮按下。我在图中找到了使用onclick()的其他资源,但我想通过Pause按钮来实现这一点。

我怎样才能实现这一点?

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2021-11-18 22:04:50

我的答案是基于答案here,它有一个很好的解释使用点击暂停动画(这你已经研究过了)。我的答案的功能与示例相同,只是修改由tkinter按钮驱动的暂停函数,而不是鼠标单击,并在代码中实现它。

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70025615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档