Bug with radio button group value on forms not in page?

This has been reported a couple of times

RadioButton.get_group_value fails when form is not shown
Init value is None for RadioButton

It’s a problem with the anvil radio button component and it’s underlying implementation
An anvil radio button only belongs to a group if it is on the screen and shares a group name with other radio buttons on the screen.
But that creates quite a hard edge!

anvil-extras just added the RadioGroup component which should solve this.
The RadioGroup acts a the coordinator for a group of radio buttons
Which means that the radio buttons don’t need to be on the screen to belong to the same group :partying_face:
https://anvil-extras.readthedocs.io/en/latest/guides/components/radio_group.html

You can use it in 3 ways:

1 - with an existing form

from anvil_extras.RadioGroup import RadioGroup

class Form(FormTemplate):
    def __init__(self, **properties):
        ...
        self.radio_group = RadioGroup(buttons=[self.radio_button_1, self.radio_button_2])
        self.radio_group.add_event_handler("change", self.radio_group_change)

    def radio_group_change(self, **event_args):
        print(self.radio_group.selected_value)

As a container

Example code snippet, but can also do this in the designer


class Form(FormTemplate):
    def __init__(self, **properties):
        ...
        self.radio_group = RadioGroup()
        self.add_component(self.radio_group)
        for option in ("foo", "bar"):
            self.radio_group.add_component(RadioButton(text=option, value=option))


With items (like dropdown components)


class Form(FormTemplate):
    def __init__(self, **properties):
        ...
        self.radio_group.items = [("Oranges", "oranges"), ("Apples", "apples")]



Here’s @duncan_richards12’s clone with the RadioGroup added


Note the new m3 theme also has a RadioGroup component

2 Likes