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 Galactica Amanda  Dune 1984 Connor MacLeod  Dune 2021 Duncan MacLeod  Serenity Heather MacLeod  Star Trek: The Motion Pictur Joe Dawson  Star Wars: A New Hope Kurgan, The  The Last Starfighter Methos  Total Recall 👉 🔴 Rachel Ellenstein  Wing Commander Ramí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 Pictu  Star Wars: A New Hope  The Last Starfighter  Total Recall 👉 🔴  Wing Commander ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▇▇ Pressed button label: Battlestar Galactica

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


textual.widgets.RadioSet class

def __init__(
    self,
    *buttons,
    name=None,
    id=None,
    classes=None,
    disabled=False
):

Bases: Container

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
Parameter Default Description
buttons
str | RadioButton
()

The labels or RadioButtons to group together.

name
str | None
None

The name of the radio set.

id
str | None
None

The ID of the radio set in the DOM.

classes
str | None
None

The CSS classes of the radio set.

disabled
bool
False

Whether the radio set is disabled or not.

Note

When a str label is provided, a RadioButton will be created from it.

BINDINGS class-attribute

BINDINGS: list[BindingType] = [
    Binding("down,right", "next_button", "", show=False),
    Binding(
        "enter,space", "toggle_button", "Toggle", show=False
    ),
    Binding("up,left", "previous_button", "", 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: RadioButton | None

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

pressed_index property

pressed_index: int

The index of the currently-pressed RadioButton, or -1 if none are pressed.

Changed class

def __init__(self, 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
Parameter Default Description
pressed
RadioButton
required

The radio button that was pressed.

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: RadioSet

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 = radio_set.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 method

def action_next_button(self):

Navigate to the next button in the set.

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

action_previous_button method

def action_previous_button(self):

Navigate to the previous button in the set.

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

action_toggle_button method

def action_toggle_button(self):

Toggle the state of the currently-selected button.