Skip to content

Switch

Switch

Bases: Widget

A switch widget that represents a boolean value.

Can be toggled by clicking on it or through its bindings.

The switch widget also contains component classes that enable more customization.

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

Key(s) Description
enter,space Toggle the switch state.

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

Class Description
switch--slider Targets the slider of the switch.

slider_pos class-attribute

The position of the slider.

value class-attribute

The value of the switch; True for on and False for off.

Changed

Bases: Message

Posted when the status of the switch changes.

Can be handled using on_switch_changed in a subclass of Switch or in a parent widget in the DOM.

Attributes:

Name Type Description
value bool

The value that the switch was changed to.

input bool

The Switch widget that was changed.

control: Switch property

Alias for self.switch.

__init__(value=False, *, animate=True, name=None, id=None, classes=None, disabled=False)

Initialise the switch.

Parameters:

Name Type Description Default
value bool

The initial value of the switch. Defaults to False.

False
animate bool

True if the switch should animate when toggled. Defaults to True.

True
name str | None

The name of the switch.

None
id str | None

The ID of the switch in the DOM.

None
classes str | None

The CSS classes of the switch.

None
disabled bool

Whether the switch is disabled or not.

False

action_toggle()

Toggle the state of the switch.

on_click()

Toggle the state of the switch.

toggle()

Toggle the switch value.

As a result of the value changing, a Switch.Changed message will be posted.