RadioSet¶
A container widget that groups RadioButton
s together.
- Focusable
- Container
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.css"
def compose(self) -> ComposeResult:
with Horizontal():
# A RadioSet built up from RadioButtons.
with RadioSet():
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:",
id="focus_me",
)
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", RadioButton).focus()
if __name__ == "__main__":
RadioChoicesApp().run()
Messages¶
Changed
¶
Bases: Message
Posted when the pressed button in the set changes.
This message can be handled using an on_radio_set_changed
method.
index
instance-attribute
¶
The index of the RadioButton
that was pressed to make the change.
pressed
instance-attribute
¶
The RadioButton
that was pressed to make the change.
radio_set
instance-attribute
¶
A reference to the RadioSet
that was changed.
__init__(radio_set, pressed)
¶
Initialise the message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pressed |
RadioButton
|
The radio button that was pressed. |
required |
Example¶
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.css"
def compose(self) -> ComposeResult:
with VerticalScroll():
with Horizontal():
with RadioSet():
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:",
id="focus_me",
)
yield RadioButton("Wing Commander")
with Horizontal():
yield Label(id="pressed")
with Horizontal():
yield Label(id="index")
def on_mount(self) -> None:
self.query_one("#focus_me", RadioButton).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()
See Also¶
- RadioSet code reference
- RadioButton