RadioGroup
A flexible container that groups multiple anvil.RadioButton components so that only one
can be selected at a time. You can either:
add individual
RadioButtoncomponents as children of the group, orset the
itemsproperty (similar to an AnvilDropdown) to auto-generate buttons.
The currently selected value is exposed via selected_value and a change event
is fired whenever the selection changes.
Usage
Use the items property (like Dropdown)
items may be a list of strings or a list of (text, value) tuples:
from anvil_extras.RadioGroup import RadioGroup
# Simple list of strings: value equals the text
self.radio_group = RadioGroup(items=["One", "Two", "Three"]) # selected_value is one of the strings
# List of (text, value) pairs: logical value is separate from the label
self.radio_group.items = [("Apples", "apples"), ("Oranges", "oranges"), ("Pears", "pears")]
# Reading the selection returns the logical value (e.g. "oranges"):
print(self.radio_group.selected_value)
# Programmatic selection: set the logical value directly
self.radio_group.selected_value = "oranges" # selects "Oranges"
Properties
- selected_value:
object | None
The logical value of the selected option. When using
itemswith(text, value)pairs, this returns thevaluepart. When using a list of strings, this is the string. Set to a matching value to programmatically select an option, or toNoneto clear.- gap:
string | float | int
Spacing between radio buttons. Accepts a number (pixels) or a CSS length string (e.g.
"12px","0.5rem").- spacing:
spacing
Standard Anvil container spacing
- direction:
enum
Layout direction of the group:
"horizontal"(default) or"vertical".- align:
enum
Alignment of buttons within the group. One of
"left","center","right","space-evenly","space-between","space-around". In horizontal layout this controls horizontal distribution; in vertical layout it controls cross-axis alignment.- visible:
bool
Whether the component is visible.
- items:
list[str] | list[tuple[str, Any]]
Use to auto-generate buttons. Strings produce labels with the same value; tuples of
(text, value)let you separate display text and logical value.- buttons:
list[anvil.RadioButton]
Assign an explicit list of existing
RadioButtoncomponents to be managed by the group. When set, event handlers are attached and the buttons are grouped so only one can be selected at a time. You can also provide this in the constructor, e.g.RadioGroup(buttons=[rb1, rb2]).
Events
- change:
Fired when the selection changes. This is the default event. Use
self.radio_group.selected_valueinside the handler to read the current value.- show:
Fired when the component is shown.
- hide:
Fired when the component is hidden.
Notes
When
itemscontains(text, value)pairs,selected_valuereturns thevalue; with a simple list of strings, it returns the string itself.Setting
selected_valueto a value that doesn’t exist will clear the selection.For tuple
items, setselected_valueto the tuple’s logical value (e.g."oranges") at any time to select that option.align="left"/"right"map to flexboxflex-start/flex-endunder the hood.