Switch#

A sliding track-and-thumb toggle for binary on/off input.

Switch — light theme Switch — dark theme

Usage#

Basic#

bs.Switch("Off", value=False)
bs.Switch("On",  value=True)

Accent colors#

bs.Switch("Primary",   accent="primary",   value=True)
bs.Switch("Secondary", accent="secondary", value=True)
bs.Switch("Info",      accent="info",      value=True)
bs.Switch("Success",   accent="success",   value=True)
bs.Switch("Warning",   accent="warning",   value=True)
bs.Switch("Danger",    accent="danger",    value=True)
Switch accent colors — light theme Switch accent colors — dark theme

Reactive binding#

Bind a Signal with signal=. The switch and signal stay in sync. When signal= is provided, value= is ignored — seed the Signal directly.

dark_mode = bs.Signal(False)
bs.Switch("Dark mode", signal=dark_mode)
dark_mode.subscribe(lambda v: bs.set_theme("dark" if v else "light"))

Custom values#

Use checked_value= and unchecked_value= when the logical values are not True/False.

bs.Switch("Theme",
    checked_value="dark",
    unchecked_value="light",
    value="light")

Disabled#

bs.Switch("Cannot change", disabled=True)
bs.Switch("Locked on",     value=True, disabled=True)
Switch disabled — light theme Switch disabled — dark theme

Events#

sw = bs.Switch("Notifications")

# Fires on every toggle
sw.on_change(lambda e: print("changed:", sw.checked))

# Fires only when switched on
sw.on_check(lambda e: print("switched on"))

# Fires only when switched off
sw.on_uncheck(lambda e: print("switched off"))

# As a Stream
sw.on_change().debounce(200).listen(lambda e: save_settings())

Programmatic control#

sw = bs.Switch("Option")

sw.checked = True          # turn on
sw.value                   # → True
sw.toggle()                # flip state

sw.disabled = True         # lock the switch

Widget sizing#

All widgets accept self-placement kwargs via **kwargs. The parent container determines which options apply — stack-based parents use stack kwargs, grid-based parents use grid kwargs. Unrecognised keys are silently ignored.

Stack#

Used inside VStack, HStack, App, and other stack containers.

fill

Fill direction: 'x', 'y', 'both', or 'none'.

expand

Grow to consume extra space in the parent. True or False.

anchor

Alignment when the widget does not fill the available slot: 'n', 's', 'e', 'w', 'center', 'nw', etc.

margin

External spacing in pixels. Accepts an integer (equal on all sides), a 2-tuple (horizontal, vertical), or a 4-tuple (left, top, right, bottom).

margin_x

Horizontal external spacing (left and right). Accepts an integer or a 2-tuple (left, right) for asymmetric spacing. Overrides the horizontal component of margin=.

margin_y

Vertical external spacing (top and bottom). Accepts an integer or a 2-tuple (top, bottom) for asymmetric spacing. Overrides the vertical component of margin=.

Grid#

Used inside a Grid container.

row / column

Zero-based row and column indices.

rowspan / columnspan

Number of rows or columns to span.

sticky

Alignment and fill within the grid cell. Any combination of 'n', 's', 'e', 'w' — e.g. 'ew' stretches horizontally, 'nsew' fills the entire cell.

margin

External spacing in pixels. Accepts an integer, a 2-tuple (horizontal, vertical), or a 4-tuple (left, top, right, bottom).

margin_x

Horizontal external spacing. Accepts an integer or (left, right).

margin_y

Vertical external spacing. Accepts an integer or (top, bottom).

API#

The complete reference for Switch lives on the Widgets API page. At a glance:

Switch

A toggle switch — on or off.

Full Example#

 1
 2with bs.App(title="Switch Demo", padding=20, gap=16) as app:
 3
 4    # Basic
 5    bs.Label("Basic", font="heading-sm")
 6    with bs.HStack(gap=24):
 7        bs.Switch("Off", value=False)
 8        bs.Switch("On",  value=True)
 9
10    # Accent colors
11    bs.Label("Accent Colors", font="heading-sm")
12    with bs.HStack(gap=16):
13        for accent in ("primary", "secondary", "info", "success", "warning", "danger"):
14            bs.Switch(accent.title(), accent=accent, value=True)
15
16    # Reactive binding
17    bs.Label("Reactive Binding", font="heading-sm")
18    with bs.VStack(gap=6):
19        dark_sig = bs.Signal(False)
20        bs.Switch("Dark mode", signal=dark_sig, accent="secondary")
21        status_lbl = bs.Label("Theme: light", accent="secondary", font="caption")
22        dark_sig.subscribe(lambda v: setattr(status_lbl, 'text', f"Theme: {'dark' if v else 'light'}"))
23
24    # Disabled
25    bs.Label("Disabled", font="heading-sm")
26    with bs.HStack(gap=24):
27        bs.Switch("Disabled off", disabled=True, value=False)
28        bs.Switch("Disabled on",  disabled=True, value=True)
29
30app.run()