Switch
An on/off toggle in the theme’s accent color. on_toggle receives the new bool.
ui.switch · Category: Forms · Full reference →


pz.column([ ui.switch(True, on_toggle, label="Autosave"), ui.switch(False, on_toggle, label="Vim mode"),], spacing=12)When to use
Section titled “When to use”- 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 pzfrom 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")Variants
Section titled “Variants”

Code
ui.switch(True, on_toggle, label="Autosave")

Code
ui.switch(False, on_toggle, label="Autosave")

Code
ui.switch(True, label="Sync (managed)")

Code
ui.switch(True, on_toggle)States and events
Section titled “States and events”

disabled=TrueCode
ui.switch(True, on_toggle, label="Locked", disabled=True)

size="sm"Code
ui.switch(True, on_toggle, label="Small", size="sm")

size="lg"Code
ui.switch(True, on_toggle, label="Large", size="lg")Base widget
Section titled “Base widget”The pz.* widget underneath: no theme, every color and size is yours.


pz.togglerCode
pz.toggler(True, "pz.toggler", on_toggle=on_toggle, accent="#a1c181", size=22)Events
Section titled “Events”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().
Handling the events
Section titled “Handling the events”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)from dataclasses import dataclass
@dataclassclass SetAutosave: on: bool
class Settings(pz.App): autosave = True
def update(self, message): match message: case SetAutosave(on): self.autosave = on
def view(self): return ui.switch(self.autosave, SetAutosave, label="Autosave")from dataclasses import dataclass
@dataclass(frozen=True)class State: autosave: bool = True
def view(state, set_state): return ui.switch(state.autosave, lambda on: set_state(autosave=on), label="Autosave")Properties and API
Section titled “Properties and API”ui.switch
Section titled “ui.switch”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).
pz.toggler
Section titled “pz.toggler”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).