Python Forum
[PyGUI] Getting values from ButtonGroup
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] Getting values from ButtonGroup
#4
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.
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()
Reply


Messages In This Thread
Getting values from ButtonGroup - by cnjosack - Mar-30-2021, 02:14 PM
RE: Getting values from ButtonGroup - by joe_momma - Mar-30-2021, 02:50 PM
RE: Getting values from ButtonGroup - by cnjosack - Mar-30-2021, 06:23 PM
RE: Getting values from ButtonGroup - by deanhystad - Mar-30-2021, 06:58 PM
RE: Getting values from ButtonGroup - by cnjosack - Apr-01-2021, 07:45 PM
RE: Getting values from ButtonGroup - by deanhystad - Apr-02-2021, 04:20 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020