RadioSet¶
Added in version 0.13.0
A container widget that groups RadioButton
s together.
- Focusable
- Container
Example¶
Simple example¶
The example below shows two radio sets, one built using a collection of radio buttons, the other a collection of simple strings.
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import RadioButton, RadioSet
class RadioChoicesApp(App[None]):
CSS_PATH = "radio_set.tcss"
def compose(self) -> ComposeResult:
with Horizontal():
# A RadioSet built up from RadioButtons.
with RadioSet(id="focus_me"):
yield RadioButton("Battlestar Galactica")
yield RadioButton("Dune 1984")
yield RadioButton("Dune 2021")
yield RadioButton("Serenity", value=True)
yield RadioButton("Star Trek: The Motion Picture")
yield RadioButton("Star Wars: A New Hope")
yield RadioButton("The Last Starfighter")
yield RadioButton(
"Total Recall :backhand_index_pointing_right: :red_circle:"
)
yield RadioButton("Wing Commander")
# A RadioSet built up from a collection of strings.
yield RadioSet(
"Amanda",
"Connor MacLeod",
"Duncan MacLeod",
"Heather MacLeod",
"Joe Dawson",
"Kurgan, [bold italic red]The[/]",
"Methos",
"Rachel Ellenstein",
"Ramírez",
)
def on_mount(self) -> None:
self.query_one("#focus_me").focus()
if __name__ == "__main__":
RadioChoicesApp().run()
Reacting to Changes in a Radio Set¶
Here is an example of using the message to react to changes in a RadioSet
:
from textual.app import App, ComposeResult
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import Label, RadioButton, RadioSet
class RadioSetChangedApp(App[None]):
CSS_PATH = "radio_set_changed.tcss"
def compose(self) -> ComposeResult:
with VerticalScroll():
with Horizontal():
with RadioSet(id="focus_me"):
yield RadioButton("Battlestar Galactica")
yield RadioButton("Dune 1984")
yield RadioButton("Dune 2021")
yield RadioButton("Serenity", value=True)
yield RadioButton("Star Trek: The Motion Picture")
yield RadioButton("Star Wars: A New Hope")
yield RadioButton("The Last Starfighter")
yield RadioButton(
"Total Recall :backhand_index_pointing_right: :red_circle:"
)
yield RadioButton("Wing Commander")
with Horizontal():
yield Label(id="pressed")
with Horizontal():
yield Label(id="index")
def on_mount(self) -> None:
self.query_one(RadioSet).focus()
def on_radio_set_changed(self, event: RadioSet.Changed) -> None:
self.query_one("#pressed", Label).update(
f"Pressed button label: {event.pressed.label}"
)
self.query_one("#index", Label).update(
f"Pressed button index: {event.radio_set.pressed_index}"
)
if __name__ == "__main__":
RadioSetChangedApp().run()
Messages¶
Bindings¶
The RadioSet
widget defines the following bindings:
Key(s) | Description |
---|---|
enter, space | Toggle the currently-selected button. |
left, up | Select the previous radio button in the set. |
right, down | Select the next radio button in the set. |
Component Classes¶
This widget has no component classes.
See Also¶
Bases: VerticalScroll
Widget for grouping a collection of radio buttons into a set.
When a collection of RadioButton
s are
grouped with this widget, they will be treated as a mutually-exclusive
grouping. If one button is turned on, the previously-on button will be
turned off.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | RadioButton
|
The labels or |
()
|
|
str | None
|
The name of the radio set. |
None
|
|
str | None
|
The ID of the radio set in the DOM. |
None
|
|
str | None
|
The CSS classes of the radio set. |
None
|
|
bool
|
Whether the radio set is disabled or not. |
False
|
|
RenderableType | None
|
Optional tooltip. |
None
|
Note
When a str
label is provided, a
RadioButton will be created from
it.
BINDINGS
class-attribute
¶
BINDINGS = [
Binding(
"down,right",
"next_button",
"Next option",
show=False,
),
Binding(
"enter,space", "toggle_button", "Toggle", show=False
),
Binding(
"up,left",
"previous_button",
"Previous option",
show=False,
),
]
Key(s) | Description |
---|---|
enter, space | Toggle the currently-selected button. |
left, up | Select the previous radio button in the set. |
right, down | Select the next radio button in the set. |
pressed_button
property
¶
The currently-pressed RadioButton
, or None
if none are pressed.
pressed_index
property
¶
The index of the currently-pressed RadioButton
, or -1 if none are pressed.
Changed
¶
Changed(radio_set, pressed)
Bases: Message
Posted when the pressed button in the set changes.
This message can be handled using an on_radio_set_changed
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
RadioButton
|
The radio button that was pressed. |
required |
ALLOW_SELECTOR_MATCH
class-attribute
instance-attribute
¶
Additional message attributes that can be used with the on
decorator.
control
property
¶
A reference to the RadioSet
that was changed.
This is an alias for Changed.radio_set
and is used by the on
decorator.
index
instance-attribute
¶
The index of the RadioButton
that was pressed to make the change.
action_next_button
¶
Navigate to the next button in the set.
Note that this will wrap around to the start if at the end.
action_previous_button
¶
Navigate to the previous button in the set.
Note that this will wrap around to the end if at the start.