lawsie / guizero

A Python 3 library to allow learners to quickly and easily create GUIs.
https://lawsie.github.io/guizero
BSD 3-Clause "New" or "Revised" License
402 stars 81 forks source link

args for widgets that support command #499

Open ThePix opened 8 months ago

ThePix commented 8 months ago

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.

martinohanlon commented 7 months ago

tbh, guizero moved away from the use of args for a few reasons:

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()
ThePix commented 7 months ago

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.

martinohanlon commented 4 months ago

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?