Similar to this question, I would like to save the value of a matplotlib slider when the button is clicked. Printing the value to the console is easy with print(), however I can't figure out how to save it to a variable. This is what I have tried, but it returns a value of zero before the user does anything.
def myFunction():
fig, ax = plt.subplots()
ax_slider = plt.axes([0.25, 0.1, 0.65, 0.03])
lag_slider = Slider(ax=ax_slider, label='lag (s)', valmin=-15, valmax=15, valinit=0)
def update(val):
lag = lag_slider.val
lag_slider.on_changed(update)
button_ax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(button_ax, 'Set Lag')
def set_lag(val):
lag = lag_slider.val
print(lag) # this prints the lag value to the console, I want to return it from the function
return lag
lag = button.on_clicked(set_lag)
return lag # this executes before the button is clicked