0

Im trying to make a gui that displays a metronome that allows the user to change the bpm using a slide on the bottom. At the moment, I believe I have figured out how to display the metronome and even get it to change the rate at which it goes back and forth however, I am still struggling with getting it to be accurate. Here is my code so far:

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

matplotlib.use('TkAgg')

fig = matplotlib.figure.Figure(figsize=(4, 5), dpi=100)
fig.add_subplot(111).plot(0,10)

range1, range2 = 10, 200
bpm, i = 80, 80

max_angle = 3000/bpm
angle = 90 + max_angle


fig, ax = plt.subplots()
xfixdata, yfixdata = 0,0
xdata, ydata = 5, 10
ln, = plt.plot([], [], 'ro-', animated=True)
plt.plot([xfixdata], [yfixdata], 'bo')

def init():
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2, 10)
    return ln,

def update(frame):
    # ydata = points[frame]
    xdata = 5
    ydata = np.sin(frame/bpm*10*np.pi)
    ln.set_data([yfixdata,ydata], [xfixdata,xdata])
    return ln,

ani = FuncAnimation(fig, update, interval=100, init_func=init, blit=True, frames=1000)
plt.axis('off')


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.Slider((range2, range1), default_value=bpm, resolution=4, tick_interval=16, orientation='h', enable_events=True, expand_x=True, key='-Slider-')],
          [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
    elif event == '-Slider-':
        bpm = values[event]



window.close()`

I was expecting that the metronome would correspond accurately with the numbers on the slider however it seems that it is doing the opposite. If someone could help me with both this problem and also could take a look at the way my metronome goes back and forth it would be greatly appreciated! Thank you.

Jason Yang
  • 11,284
  • 2
  • 9
  • 23

0 Answers0