# Radio group

> Mutually exclusive options as radio buttons. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
A few round options where picking one unpicks the others.
:::

A set of radio buttons where one option is selected. `options` are strings or `(value, label)` pairs; `on_select` receives the value. `horizontal=True` lays them out in a row.

`ui.radio_group` · Category: [Forms](https://albakore.github.io/pyzed/library/#forms) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#radio_group)

**Code:**

```python
pz.row([
    ui.radio_group(["Spaces", "Tabs"], "Spaces", on_select),
    ui.radio_group([("lf", "LF"), ("crlf", "CRLF"), ("cr", "CR")], "lf", on_select, horizontal=True),
], spacing=40, align="center")
```

## When to use

- When the options need a label each and should all be visible (indentation: spaces or tabs).
- For many options use a [Select](https://albakore.github.io/pyzed/library/select/).

## Usage

```python
import pyzed as pz
from pyzed import ui

class Settings(pz.App):
    def __init__(self):
        self.indent = "spaces"

    def view(self):
        return ui.radio_group([("spaces", "Spaces"), ("tabs", "Tabs")], self.indent,
                              lambda value: setattr(self, "indent", 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.
:::

### Direction

```python
ui.radio_group(["Spaces", "Tabs"], "Spaces", on_select)
```

```python
ui.radio_group(["LF", "CRLF", "CR"], "LF", on_select, horizontal=True)
```

```python
ui.radio_group(["Small", "Medium", "Large"], "Medium", on_select, horizontal=True, spacing=16)
```

### Options

```python
ui.radio_group([("lf", "Unix (LF)"), ("crlf", "Windows (CRLF)")], "crlf", on_select)
```

```python
ui.radio_group(["Yes", "No"], None, on_select, horizontal=True)
```

### States and events

```python
ui.radio_group(["LF", "CRLF", "CR"], "LF", on_select, horizontal=True, disabled={"CR"})
```

```python
ui.radio_group(["Spaces", "Tabs"], "Spaces", on_select,
               descriptions={"Spaces": "Indent with 4 spaces", "Tabs": "One tab per level"})
```

### Base widget

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

```python
pz.row([pz.radio("Cats", True, on_select, accent="#e5c07b"),
        pz.radio("Dogs", False, on_select, accent="#e5c07b")], spacing=16)
```

## Events

Radio 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 | Of | Handler gets | When |
| --- | --- | --- | --- |
| `on_select` | `ui.radio_group`, `pz.radio` | `(value)` | Callable that receives the chosen value. |

**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 Settings(pz.App):
    def __init__(self):
        self.indent = "spaces"

    def set_indent(self, value):         # on_select gets the value, not the label
        self.indent = value

    def view(self):
        return ui.radio_group([("spaces", "Spaces"), ("tabs", "Tabs")], self.indent, self.set_indent)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Indent:
    value: str

class Settings(pz.App):
    indent = "spaces"

    def update(self, message):
        match message:
            case Indent(value):
                self.indent = value

    def view(self):
        return ui.radio_group([("spaces", "Spaces"), ("tabs", "Tabs")], self.indent, Indent)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    indent: str = "spaces"

def view(state, set_state):
    return ui.radio_group([("spaces", "Spaces"), ("tabs", "Tabs")], state.indent,
                          lambda value: set_state(indent=value), horizontal=True)
```

## Tips

:::tip
Once clicked or reached with Tab the group has the keyboard focus: the arrows pick the previous or next choice (skipping disabled ones) and Home/End the first and last.
:::

## Properties and API

### `ui.radio_group`

```python
radio_group(
    options,
    selected,
    on_select=None,
    *,
    horizontal=False,
    spacing=8,
    disabled=False,
    descriptions=None,
    id=None,
    theme=None,
)
```

Mutually exclusive options as radio buttons.

| Property | Default | Description |
| --- | --- | --- |
| `options` | *required* | Strings or `(value, label)` pairs. |
| `selected` | *required* | Current value. |
| `horizontal` | `False` | In a row instead of a column. |
| `spacing` | `8` | Space between children, in pixels. |
| `disabled` | `False` | `True`, or the values to disable. |
| `descriptions` | `None` | Maps values to a muted line under their label. |
| `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_select` ([events](#events)).

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

### `pz.radio`

```python
radio(
    label,
    selected,
    on_select=None,
    *,
    size=None,
    text_size=None,
    spacing=None,
    accent=None,
    bg=None,
    border_color=None,
    color=None,
)
```

A radio button. Group several yourself (or use `ui.radio_group`); `on_select` is sent when it is clicked.

| Property | Default | Description |
| --- | --- | --- |
| `label` | *required* | Label text. |
| `selected` | *required* | Whether it is checked. |
| `size` | `None` | Size. |
| `text_size` | `None` | Text size. |
| `spacing` | `None` | Space between the circle and the text. |
| `accent` | `None` | Accent color (active part). |
| `bg` | `None` | Background color (`"#rrggbb[aa]"`, `(r, g, b[, a])` or `"transparent"`). |
| `border_color` | `None` | Border color. |
| `color` | `None` | Text or icon color. |

Events: `on_select` ([events](#events)).

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

## Related

- [Segmented](https://albakore.github.io/pyzed/library/segmented/): A group of mutually exclusive text buttons.
- [Select](https://albakore.github.io/pyzed/library/select/): A dropdown to choose one option.
- [Checkbox](https://albakore.github.io/pyzed/library/checkbox/): A checkbox with an optional label.
