Open ThePix opened 9 months ago
tbh, guizero moved away from the use of args
for a few reasons:
args
is evaluated when the widget is created, and you cant pass a reference to a variablelambda
e.g. if you wanted to send 2 values to sliders command, the slider value plus an index, you could do this:
from guizero import App, Slider, TextBox
def slider_changed(slider_value, index):
textbox.value = str(slider_value) + ":" + str(index)
app = App()
slider = Slider(app, command=lambda: slider_changed(slider.value, 1))
textbox = TextBox(app)
app.display()
A lambda is a reasonable approach. But I would suggest updating the docs to say you can do that. Currently says "The name of a function to call when the slider value is changed". some example code as you do in your response would also be very useful.
If you want to consider this closed, I have no problem with that.
Lets leave it open until we agree what to do.
I am all for:
. updating the docs to reflect the use of lambda when passing arguments to callbacks
. deprecating the use of args
- this maybe more controversial @lawsie - thoughts?
The PushButton widget has an args parameter that can be used to send arguments to the command function. The slider widget, for example, also supports a command function, so it would make sense if it also supports args.
An example use case would be for a list of similar things, each needing a slider and a button. These could be added in a loop. Each button can use the same command function, and by giving the iterator index in the args the button can be identified. I want to be able to do tat with the slider.
Also applies to ListBox, TextBox, CheckBox, Combo.