[matpltlib]Basic question about callback function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [matpltlib]Basic question about callback function (/thread-18503.html) |
[matpltlib]Basic question about callback function - schniefen - May-20-2019 I'm learning about using the slider and button widgets and am studying the example given in the link. Slider Demo import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons fig, ax = plt.subplots() plt.subplots_adjust(left=0.25, bottom=0.25) t = np.arange(0.0, 1.0, 0.001) a0 = 5 f0 = 3 delta_f = 5.0 s = a0 * np.sin(2 * np.pi * f0 * t) l, = plt.plot(t, s, lw=2) plt.axis([0, 1, -10, 10]) axcolor = 'lightgoldenrodyellow' axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor) axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor) sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f) samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) def update(val): amp = samp.val freq = sfreq.val l.set_ydata(amp*np.sin(2*np.pi*freq*t)) fig.canvas.draw_idle() sfreq.on_changed(update) samp.on_changed(update) resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975') def reset(event): sfreq.reset() samp.reset() button.on_clicked(reset) rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor) radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) def colorfunc(label): l.set_color(label) fig.canvas.draw_idle() radio.on_clicked(colorfunc) plt.show()I don't understand the parameters of the two first functions in the example nor the fact that they don't have any return statements. It seems like the parameters aren't used in the function, yet they're needed. I'd appreciate a clarification on what is going on. Thanks! RE: [matpltlib]Basic question about callback function - Yoriz - May-20-2019 A callback function from a slider that's connected using on_changed must except a int which is the value that represents the slider position.https://matplotlib.org/api/widgets_api.html?highlight=slider#matplotlib.widgets.Slider.on_changed Wrote:on_changed(self, func) The parameter val is not used in callback function update because both sliders are using the same callback, and then obtaining the value from both sliders usingamp = samp.val freq = sfreq.valA callback function from a button that's connected using on_clicked must except a connection id which can be used to disconnect the callback.https://matplotlib.org/api/widgets_api.html?highlight=slider#matplotlib.widgets.Button.on_clicked Wrote:on_clicked(self, func)The reset callback is not using the connection id to disconnect the callback because the button is required to stay active for future reset events.Callback functions don't have return values because there is no where in the code for them to return to, in gui progams an event loop redraws the gui and waits for an event to happen like a button click, it then calls the connected event function to do whats required, and then redraws and waits for another event. RE: [matpltlib]Basic question about callback function - schniefen - May-20-2019 So val gets assigned a value through the on_clicked function? How could I access the integer on_changed oron_clicked returns?
RE: [matpltlib]Basic question about callback function - Yoriz - May-20-2019 When the slider is altered it triggers an event that calls the connected event handler with the val of the slider,that is why the event handler function must except the val argument.To access val in the event handler function just use the val attribute like shown below.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f) sfreq.on_changed(update) def update(val): print(val) |