Mar-30-2021, 06:58 PM
Regardless of the type you provide for "value" it becomes a string when you call ButtonGroup.value. It really limits the usefulness of providing a value for the button.
I wanted to provide a function as the second argument. When I asked the ButtonGroup to return the options the functions were still there. When I selected a button the value was the __repr__ string for the function. So I made my own map between key and value.
What do you mean by this?
In the code below I use "1" and "0" when checking the group button value and it works fine. I think it is stupid code, but it works.
I wanted to provide a function as the second argument. When I asked the ButtonGroup to return the options the functions were still there. When I selected a button the value was the __repr__ string for the function. So I made my own map between key and value.
from guizero import App, ButtonGroup functions = { 'A': lambda: print('Aye'), 'B': lambda: print('Bee'), 'C': lambda: print('Sea')} choices = list(functions.keys()) def selected(): functions[row_choice.value]() app = App(title="Test", width=100, height=100) row_choice = ButtonGroup( app, options=choices, selected=choices[0], command=selected) app.display()
What do you mean by this?
Quote: I tried the character "1" as well but that didn't work either.
In the code below I use "1" and "0" when checking the group button value and it works fine. I think it is stupid code, but it works.
from guizero import App, ButtonGroup def selection(): if row_choice.value == '1': print('GPIO.output(18,GPIO.HIGH)') elif row_choice.value == '0': print('GPIO.output(18,GPIO.LOW)') app = App(title="Test", width=300, height=200, layout="grid") row_choice = ButtonGroup( app, options=[ ["Long wire", 1], ["SlimJim", 2], ["10m", 3], ["Off", 0]], selected='0', horizontal=True, grid=[1,2], align="left", command=selection) app.display()If you compare the button value, and the button value is a string, it makes more sense to use a descriptive string like "Off" instead of "0".
from guizero import App, ButtonGroup def selection(): if row_choice.value == 'Long wire': print('GPIO.output(18,GPIO.HIGH)') elif row_choice.value == 'Off': print('GPIO.output(18,GPIO.LOW)') app = App(title="Test", width=300, height=200, layout="grid") row_choice = ButtonGroup( app, options=["Long wire", "SlimJim", "10m", "Off"], selected='Off', horizontal=True, grid=[1,2], align="left", command=selection) app.display()