Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 [matpltlib]Basic question about callback function schniefen Programmer named Tim Posts: 17 Threads: 11 Joined: Mar 2019 Reputation: 0 Likes received: 0 #1 May-20-2019, 07:20 PM (This post was last modified: May-20-2019, 07:33 PM by Yoriz. Edited 1 time in total. Edit Reason: Prefix added, Code added from link ) 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! Yoriz Posts: 1,226 Threads: 28 Joined: Sep 2016 Reputation: 119 Likes received: 280 #2 May-20-2019, 08:04 PM 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.h...on_changed Wrote:on_changed(self, func) When the slider value is changed call func with the new slider value Parameters: func : callable Function to call when slider is changed. The function must accept a single float as its arguments. Returns: cid : int Connection id (which can be used to disconnect 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 using ``` amp = samp.val freq = sfreq.val```A 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.h...on_clicked Wrote:on_clicked(self, func) Connect the callback function func to button click events. Returns a connection id, which can be used to disconnect the callback. 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. schniefen likes this post schniefen Programmer named Tim Posts: 17 Threads: 11 Joined: Mar 2019 Reputation: 0 Likes received: 0 #3 May-20-2019, 09:39 PM (This post was last modified: May-20-2019, 09:39 PM by schniefen. Edited 2 times in total.) So `val` gets assigned a value through the `on_clicked` function? How could I access the integer `on_changed` or`on_clicked` returns? Yoriz Posts: 1,226 Threads: 28 Joined: Sep 2016 Reputation: 119 Likes received: 280 #4 May-20-2019, 09:47 PM 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) ``` schniefen likes this post « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post GUI freezes while executing a callback funtion when a button is pressed abi17124 5 1,420 Jul-10-2019, 12:48 AM Last Post: FullOfHelp Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 11,035 Aug-05-2018, 11:01 PM Last Post: woooee pygtk2, how to disconnect all callback of widget or window event ? harun2525 1 1,558 Feb-19-2017, 11:44 PM Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)