Skip to content

Toggle button

ToggleButton

Bases: Static

Base toggle button widget.

Warning

ToggleButton should be considered to be an internal class; it exists to serve as the common core of Checkbox and RadioButton.

BINDINGS: ClassVar[list[BindingType]] class-attribute

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

BUTTON_INNER: str class-attribute

The character used for the inside of the button.

BUTTON_LEFT: str class-attribute

The character used for the left side of the toggle button.

BUTTON_RIGHT: str class-attribute

The character used for the right side of the toggle button.

COMPONENT_CLASSES: ClassVar[set[str]] class-attribute

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

label: Text property

The label associated with the button.

Changed

Bases: Message

Posted when the value of the toggle button changes.

value instance-attribute

The value of the toggle button after the change.

__init__(toggle_button, value)

Initialise the message.

Parameters:

Name Type Description Default
toggle_button ToggleButton

The toggle button sending the message.

required
value bool

The value of the toggle button.

required

__init__(label='', value=False, button_first=True, *, name=None, id=None, classes=None, disabled=False)

Initialise the toggle.

Parameters:

Name Type Description Default
label TextType

The label for the toggle.

''
value bool

The initial value of the toggle. Defaults to False.

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

action_toggle()

Toggle the value of the widget when called as an action.

This would normally be used for a keyboard binding.

on_click()

Toggle the value of the widget when clicked with the mouse.

render()

Render the content of the widget.

Returns:

Type Description
RenderResult

The content to render for the widget.

toggle()

Toggle the value of the widget.

watch_value()

React to the value being changed.

When triggered, the CSS class -on is applied to the widget if value has become True, or it is removed if it has become False. Subsequently a related Changed event will be posted.