# Toggle group

> An icon-only segmented control. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/toggle-group/

:::tip[In plain words]
A row of icon buttons where only one can be on, like choosing left, center or right alignment.
:::

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](https://albakore.github.io/pyzed/library/#actions) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#toggle_group)

**Code:**

```python
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

- For a small set of mutually exclusive modes that have clear icons.
- With text labels, use [Segmented](https://albakore.github.io/pyzed/library/segmented/) instead.

## Usage

```python
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),
        )
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Options

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

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

```python
ui.toggle_group([("b", "bold"), ("i", "italic"), ("u", "underline"), ("s", "strikethrough")],
                "b", on_select)
```

### Selection

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

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

```python
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
                None, on_select)
```

### States and events

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

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

```python
ui.toggle_group([("left", "align-left"), ("center", "align-center"), ("right", "align-right")],
                "left", on_select, disabled={"right"})
```

## 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](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| 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

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](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
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)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class 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 value
```

**Functional:**

```python
from 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

### `ui.toggle_group`

```python
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](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#toggle_group)

## Related

- [Segmented](https://albakore.github.io/pyzed/library/segmented/): A group of mutually exclusive text buttons.
- [Radio group](https://albakore.github.io/pyzed/library/radio-group/): Mutually exclusive options as radio buttons.
- [Icon button](https://albakore.github.io/pyzed/library/icon-button/): A square button with just an icon and an optional tooltip.
