Toggle group
A row of icon buttons where exactly one is selected, like text alignment in an editor. options are (value, icon) pairs and on_select receives the chosen value.
ui.toggle_group · Category: Actions · Full reference →


pz.row([ ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], "center", on_align), ui.toggle_group([("list", "list"), ("grid", "layout-grid")], "grid", on_layout),], spacing=16)When to use
Section titled “When to use”- For a small set of mutually exclusive modes that have clear icons.
- With text labels, use Segmented instead.
import pyzed as pzfrom pyzed import ui
class Toolbar(pz.App): def __init__(self): self.align = "left"
def view(self): return ui.toggle_group( [("left", "align-left"), ("center", "align-center"), ("right", "align-right")], self.align, lambda value: setattr(self, "align", value), )Variants
Section titled “Variants”Options
Section titled “Options”

Code
ui.toggle_group([("list", "list"), ("grid", "layout-grid")], "list", on_select)

Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], "center", on_select)

Code
ui.toggle_group([("b", "bold"), ("i", "italic"), ("u", "underline"), ("s", "strikethrough")], "b", on_select)Selection
Section titled “Selection”

selected="left"Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], "left", on_select)

selected="right"Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], "right", on_select)

selected=NoneCode
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], None, on_select)States and events
Section titled “States and events”A third item in an option adds a tooltip: (value, icon, tooltip).


multiple=TrueCode
ui.toggle_group([("b", "bold"), ("i", "italic"), ("u", "underline")], {"b", "u"}, on_select, multiple=True)

disabled={"right"}Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], "left", on_select, disabled={"right"})Events
Section titled “Events”Toggle group 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 | Handler gets | When |
|---|---|---|
on_select |
(value) |
Callable that receives the chosen item. |
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 Toolbar(pz.App): def __init__(self): self.align = "left"
def set_align(self, value): # on_select gets the value of the pressed option self.align = value
def view(self): return ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], self.align, self.set_align)from dataclasses import dataclass
@dataclassclass Align: value: str
class Toolbar(pz.App): align = "left"
def update(self, message): match message: case Align(value): self.align = value
def view(self): return ui.toggle_group([("left", "align-left"), ("center", "align-center")], self.align, Align) # the class builds the message from the valuefrom dataclasses import dataclass
@dataclass(frozen=True)class State: align: str = "left"
def view(state, set_state): return ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")], state.align, lambda value: set_state(align=value))Properties and API
Section titled “Properties and API”ui.toggle_group
Section titled “ui.toggle_group”toggle_group( options, selected, on_select=None, *, disabled=False, multiple=False, theme=None,)An icon-only segmented control.
| Property | Default | Description |
|---|---|---|
options |
required | (value, icon_name) or (value, icon_name, tooltip). |
selected |
required | Current value (a collection with multiple). |
disabled |
False |
True, or the values to disable. |
multiple |
False |
Each button toggles its value on or off. |
theme |
None |
Theme to use instead of the current one (pyzed.theme.current()). |
Events: on_select (events).