# Alert

> A message with one button. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/alert/

:::tip[In plain words]
A message box with a single OK button.
:::

A message with a single OK button. `kind` (`"info"`, `"success"`, `"warning"`, `"error"`) adds an icon.

`ui.alert` · Category: [Dialogs](https://albakore.github.io/pyzed/library/#dialogs) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#alert)

**Code:**

```python
ui.dialog_layer(main_view(), ui.alert("The export finished: 128 files were written.", on_close,
                                      title="Export", kind="success", width=360))
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- To report something the user must acknowledge (an export finished, a file could not be opened).

## Usage

```python
from pyzed import ui

ui.dialog_layer(main_view(), ui.alert("The export finished: 128 files were written.", on_close,
                                      title="Export", kind="success", width=360))
```

## 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.
:::

### Kind

```python
ui.alert("The file was moved to the trash.", on_close, width=320)
```

```python
ui.alert("A new version is available.", on_close, kind="info", width=320)
```

```python
ui.alert("128 files were exported.", on_close, kind="success", title="Export", width=320)
```

```python
ui.alert("The disk is almost full.", on_close, kind="warning", width=320)
```

```python
ui.alert("notes.py could not be opened.", on_close, kind="error", title="Error", width=320)
```

### Options

```python
ui.alert("Shortcuts changed in this version.", on_close, ok_label="Got it", width=320)
```

## Events

Alert 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_close` | `()` | The button and the close icon. |

**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 Export(pz.App):
    def __init__(self):
        self.done = True

    def view(self):
        alert = ui.alert("128 files were written.", lambda: setattr(self, "done", False),   # on_close
                         title="Export", kind="success")
        return ui.dialog_layer(main_view(), alert if self.done else None)
```

**Declarative:**

```python
class Export(pz.App):
    done = True

    def update(self, message):
        if message == "ok":
            self.done = False

    def view(self):
        return ui.dialog_layer(main_view(), ui.alert("128 files were written.", "ok", title="Export")
                               if self.done else None)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    done: bool = True

def view(state, set_state):
    alert = ui.alert("128 files were written.", lambda: set_state(done=False), title="Export")
    return ui.dialog_layer(main_view(), alert if state.done else None)
```

## Properties and API

### `ui.alert`

```python
alert(message, on_close, *, title=None, kind=None, ok_label=None, width=400, theme=None)
```

A message with one button.

| Property | Default | Description |
| --- | --- | --- |
| `message` | *required* | Text or widget. |
| `title` | `None` | Title. |
| `kind` | `None` | `"info"`, `"success"`, `"warning"` or `"error"` adds an icon. |
| `ok_label` | `None` | Button text (default: OK, translated). |
| `width` | `400` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

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

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

## Related

- [Confirm](https://albakore.github.io/pyzed/library/confirm/): A yes/no question, red for destructive actions.
- [Dialog layer](https://albakore.github.io/pyzed/library/dialog-layer/): Centers a dialog over your view with a dimmed backdrop.
- [Toast](https://albakore.github.io/pyzed/library/toast/): Notifications stacked in the bottom-right corner.
