Skip to content

RadioButton

Added in version 0.13.0

A simple radio button which stores a boolean value.

  • Focusable
  • Container

A radio button is best used with others inside a RadioSet.

Example

The example below shows radio buttons, used within a RadioSet.

RadioChoicesApp ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ Battlestar Galactica Dune 1984 Dune 2021 Serenity Star Trek: The Motion Picture Star Wars: A New Hope The Last Starfighter Total Recall 👉 🔴 Wing Commander ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

from textual.app import App, ComposeResult
from textual.widgets import RadioButton, RadioSet


class RadioChoicesApp(App[None]):
    CSS_PATH = "radio_button.tcss"

    def compose(self) -> ComposeResult:
        with RadioSet():
            yield RadioButton("Battlestar Galactica")
            yield RadioButton("Dune 1984")
            yield RadioButton("Dune 2021", id="focus_me")
            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")

    def on_mount(self) -> None:
        self.query_one(RadioSet).focus()


if __name__ == "__main__":
    RadioChoicesApp().run()
Screen {
    align: center middle;
}

RadioSet {
    width: 50%;
}

Reactive Attributes

Name Type Default Description
value bool False The value of the radio button.

Messages

Bindings

The radio button widget defines the following bindings:

Key(s) Description
enter, space Toggle the value.

Component Classes

The checkbox widget inherits the following component classes:

Class Description
toggle--button Targets the toggle button itself.
toggle--label Targets the text label of the toggle button.

See Also


Bases: ToggleButton

A radio button widget that represents a boolean value.

Note

A RadioButton is best used within a RadioSet.

Parameters:

Name Type Description Default

label

TextType

The label for the toggle.

''

value

bool

The initial value of the toggle.

False

button_first

bool

Should the button come before the label, or after?

True

name

str | None

The name of the toggle.

None

id

str | None

The ID of the toggle in the DOM.

None

classes

str | None

The CSS classes of the toggle.

None

disabled

bool

Whether the button is disabled or not.

False

tooltip

RenderableType | None

RenderableType | None = None,

None

BUTTON_INNER class-attribute instance-attribute

BUTTON_INNER = '●'

The character used for the inside of the button.

Changed

Changed(toggle_button, value)

Bases: Changed

Posted when the value of the radio button changes.

This message can be handled using an on_radio_button_changed method.

Parameters:

Name Type Description Default

toggle_button

ToggleButton

The toggle button sending the message.

required

value

bool

The value of the toggle button.

required

control property

control

radio_button property

radio_button

The radio button that was changed.