正如标题所说,我正在尝试制作一个节拍器动画,但是,我不确定从这一点上做什么。现在,我已经成功地制作了一个从固定点上下移动的棍子动画,但是,我遇到的问题是使棍子在半圆内前后移动(就像节拍器一样)。以下是我目前为止的代码
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import PySimpleGUI as sg
import matplotlib
i = 80
matplotlib.use('TkAgg')
fig = matplotlib.figure.Figure(figsize=(5, 4), dpi=100)
fig.add_subplot(111).plot(1,4)
points = (1, 5, 10, 10, 5, 1)
fig, ax = plt.subplots()
xfixdata, yfixdata = 10, 5
xdata, ydata = 5, 10
ln, = plt.plot([], [], 'ro-', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo', ms=10)
def init():
ax.set_xlim(0, 20)
ax.set_ylim(0, 20)
return ln,
def update(frame):
# ydata = points[frame]
ydata = np.sin(1 + (1 + 2*frame))
ln.set_data([xfixdata,xdata], [yfixdata,ydata])
return ln,
ani = FuncAnimation(fig, update, interval=i, init_func=init, blit=True)
plt.axis('on')
def draw_figure(canvas, figure):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure_canvas_agg
#Gui Code
# define the window layout
layout = [[sg.Text('UI Test')],
[sg.Canvas(key='-CANVAS-')],
[sg.Button('Speed Up'), sg.Button('Slow Down')],
[sg.Text(i)],
[sg.Button('Exit')]
]
# create the form and show it without the plot
window = sg.Window('UI Test For Conductor Robot', layout, finalize=True,
element_justification='center', font='Helvetica 18')
# add the plot to the window
fig_canvas_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig)
event, values = window.read()
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "Speed Up":
i = i + 10
window.close()
另外,我遇到的另一个问题是制作一个按钮来提高动画的速度。正如您在代码中所看到的,我创建了一个I值来存储值80。然后,我将间隔设置为I,并制作了一个按钮,当按下该按钮时,该按钮会将I值增加10,因此,减慢了动画的速度。它不起作用。如果你能帮助这将是很好的,但是,我目前关心的主要问题是制作节拍器的动画。谢谢你的阅读!
1条答案
按热度按时间db2dz4w81#
对于此处使用的
PySimpleGUI
,不需要库Matplotlib
。示例代码这里只是演示动画,bpm是不正确或准确的,也没有声音在这里。