0

I have multiple similar Sliders, and want to call the function update with a certain argument when a the corresponding slider is changed. Passing additional parameters should work similarly to other widgets, e.g., on_clicked() of Button.

Simplified example using base code from Slider demo:

def update(val, string):
    line.set_ydata(f(t, amp_slider.val, freq_slider.val))
    print(string)
    fig.canvas.draw_idle()

freq_slider.on_changed(update("Frequency updated"))
amp_slider.on_changed(update("Amplitude updated"))

The following doesn't pass the updated val at all, and hence obviously doesn't work. According to the documentation on_changed only accepts a callable function as a parameter. Is there a way to solve this without somehow incorporating mpl_connect, e.g. using a lambda function?

1 Answer 1

1

You can use lambda to call update with additional parameters. Note that new_val is actually the current value of the slider here.

freq_slider.on_changed(lambda new_val: update(new_val, "Frequency updated"))

Below is an example using modified code from before

def update(val, string="Updated without additional string param"):
    line.set_ydata(f(t, amp_slider.val, freq_slider.val))
    print(string)
    fig.canvas.draw_idle()


freq_slider.on_changed(lambda new_val: update(new_val, "Frequency updated"))
amp_slider.on_changed(update)  # no string arg
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.