Skip to content

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 →

Toggle groupToggle group
  • For a small set of mutually exclusive modes that have clear icons.
  • With text labels, use Segmented instead.
import pyzed as pz
from 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),
)
Toggle group: Two optionsToggle group: Two options
Two options
Code
ui.toggle_group([("list", "list"), ("grid", "layout-grid")], "list", on_select)
Toggle group: Three optionsToggle group: Three options
Three options
Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
"center", on_select)
Toggle group: Four optionsToggle group: Four options
Four options
Code
ui.toggle_group([("b", "bold"), ("i", "italic"), ("u", "underline"), ("s", "strikethrough")],
"b", on_select)
Toggle group: selected="left"Toggle group: selected="left"
selected="left"
Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
"left", on_select)
Toggle group: selected="right"Toggle group: selected="right"
selected="right"
Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
"right", on_select)
Toggle group: selected=NoneToggle group: selected=None
selected=None
Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
None, on_select)

A third item in an option adds a tooltip: (value, icon, tooltip).

Toggle group: multiple=TrueToggle group: multiple=True
multiple=True
Code
ui.toggle_group([("b", "bold"), ("i", "italic"), ("u", "underline")], {"b", "u"}, on_select, multiple=True)
Toggle group: disabled={"right"}Toggle group: disabled={"right"}
disabled={"right"}
Code
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
"left", on_select, disabled={"right"})

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().

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)
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).

Full reference →