Skip to content

RadioSet

Added in version 0.13.0

A container widget that groups RadioButtons 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.

RadioChoicesApp ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Battlestar GalacticaAmanda Dune 1984Connor MacLeod Dune 2021Duncan MacLeod SerenityHeather MacLeod Star Trek: The Motion PicturJoe Dawson Star Wars: A New HopeKurgan, The The Last StarfighterMethos Total Recall 👉 🔴Rachel Ellenstein Wing CommanderRamírez ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

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()
Screen {
    align: center middle;
}

Horizontal {
    align: center middle;
    height: auto;
}

RadioSet {
    width: 45%;
}

Reacting to Changes in a Radio Set

Here is an example of using the message to react to changes in a RadioSet:

RadioSetChangedApp ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Battlestar Galactica Dune 1984 Dune 2021 Serenity Star Trek: The Motion Pictur Star Wars: A New Hope The Last Starfighter Total Recall 👉 🔴 Wing Commander ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ Pressed button label: Battlestar Galactica Pressed button index: 0

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()
VerticalScroll {
    align: center middle;
}

Horizontal {
    align: center middle;
    height: auto;
}

RadioSet {
    width: 45%;
}

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 RadioButtons 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

buttons

str | RadioButton

The labels or RadioButtons to group together.

()

name

str | None

The name of the radio set.

None

id

str | None

The ID of the radio set in the DOM.

None

classes

str | None

The CSS classes of the radio set.

None

disabled

bool

Whether the radio set is disabled or not.

False

tooltip

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

pressed_button

The currently-pressed RadioButton, or None if none are pressed.

pressed_index property

pressed_index

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

pressed

RadioButton

The radio button that was pressed.

required

ALLOW_SELECTOR_MATCH class-attribute instance-attribute

ALLOW_SELECTOR_MATCH = {'pressed'}

Additional message attributes that can be used with the on decorator.

control property

control

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

index = pressed_index

The index of the RadioButton that was pressed to make the change.

pressed instance-attribute

pressed = pressed

The RadioButton that was pressed to make the change.

radio_set instance-attribute

radio_set = radio_set

A reference to the RadioSet that was changed.

action_next_button

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

action_previous_button()

Navigate to the previous button in the set.

Note that this will wrap around to the end if at the start.

action_toggle_button

action_toggle_button()

Toggle the state of the currently-selected button.