Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

args for widgets that support command #499

Open
ThePix opened this issue Feb 20, 2024 · 3 comments
Open

args for widgets that support command #499

ThePix opened this issue Feb 20, 2024 · 3 comments

Comments

@ThePix
Copy link

ThePix commented Feb 20, 2024

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
Copy link
Collaborator

martinohanlon commented Mar 2, 2024

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

  • people found it confusing, as args is evaluated when the widget is created, and you cant pass a reference to a variable
  • some widgets (like slider) already pass back a argument (in this case, the value of the slider)
  • but mainly because you can achieve the same using lambda

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
Copy link
Author

ThePix commented Mar 5, 2024

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
Copy link
Collaborator

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants