Skip to content

Switch

An on/off toggle in the theme’s accent color. on_toggle receives the new bool.

ui.switch · Category: Forms · Full reference →

SwitchSwitch
  • For settings that take effect immediately (Wi-Fi, autosave, dark mode).
  • Put it on the right of a Field to build settings screens.
import pyzed as pz
from pyzed import ui
class Settings(pz.App):
def __init__(self):
self.autosave = True
def view(self):
return ui.switch(self.autosave, lambda on: setattr(self, "autosave", on), label="Autosave")
Switch: OnSwitch: On
On
Code
ui.switch(True, on_toggle, label="Autosave")
Switch: OffSwitch: Off
Off
Code
ui.switch(False, on_toggle, label="Autosave")
Switch: Read-only (no on_toggle)Switch: Read-only (no on_toggle)
Read-only (no on_toggle)
Code
ui.switch(True, label="Sync (managed)")
Switch: No labelSwitch: No label
No label
Code
ui.switch(True, on_toggle)
Switch: disabled=TrueSwitch: disabled=True
disabled=True
Code
ui.switch(True, on_toggle, label="Locked", disabled=True)
Switch: size="sm"Switch: size="sm"
size="sm"
Code
ui.switch(True, on_toggle, label="Small", size="sm")
Switch: size="lg"Switch: size="lg"
size="lg"
Code
ui.switch(True, on_toggle, label="Large", size="lg")

The pz.* widget underneath: no theme, every color and size is yours.

Switch: pz.togglerSwitch: pz.toggler
pz.toggler
Code
pz.toggler(True, "pz.toggler", on_toggle=on_toggle, accent="#a1c181", size=22)

Switch has 1 event of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

Event Of Handler gets When
on_toggle ui.switch, pz.toggler (checked) Callable that receives the new bool (or a message to send).

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class Settings(pz.App):
def __init__(self):
self.autosave = True
def set_autosave(self, on): # on_toggle: the new state
self.autosave = on
def view(self):
return pz.column([
ui.switch(self.autosave, self.set_autosave, label="Autosave"),
ui.label("Saving as you type" if self.autosave else "Save with Ctrl+S"),
], spacing=8)
switch(
checked,
on_toggle=None,
*,
label=None,
size='md',
disabled=False,
id=None,
theme=None,
)

An on/off switch.

Property Default Description
checked required Current state.
label None Label text.
size "md" "sm", "md" or "lg".
disabled False Greys it out and ignores clicks.
id None Stable identifier used to focus or scroll it with a Command.
theme None Theme to use instead of the current one (pyzed.theme.current()).

Events: on_toggle (events).

Full reference →

toggler(
checked,
label=None,
*,
on_toggle=None,
size=None,
text_size=None,
accent=None,
bg=None,
border_color=None,
knob_color=None,
color=None,
)

An on/off switch.

Property Default Description
checked required Current state.
label None Label text.
size None Switch height.
text_size None Text size.
accent None Accent color (active part).
bg None Background color ("#rrggbb[aa]", (r, g, b[, a]) or "transparent").
border_color None Border color.
knob_color None Knob color.
color None Text or icon color.

Events: on_toggle (events).

Full reference →