# Confirm

> A yes/no question, red for destructive actions. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

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

:::tip[In plain words]
Asks “are you sure?” before doing something that can't be undone.
:::

Asks a yes/no question. `danger=True` paints the confirm button red; `confirm_label` and `cancel_label` rename the buttons.

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

**Code:**

```python
ui.dialog_layer(main_view(), ui.confirm(
    "Delete notes.py? This can't be undone.", on_delete, on_cancel,
    title="Delete file", danger=True, confirm_label="Delete", 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

- Before destructive or irreversible actions: delete, overwrite, discard.

## Usage

```python
from pyzed import ui

ui.dialog_layer(main_view(), ui.confirm(
    "Delete notes.py? This can't be undone.", on_delete, on_cancel,
    title="Delete file", danger=True, confirm_label="Delete", 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.confirm("Close all tabs?", on_confirm, on_cancel, title="Close tabs", width=320)
```

```python
ui.confirm("Delete notes.py?", on_delete, on_cancel, title="Delete file", danger=True,
           confirm_label="Delete", width=320)
```

```python
ui.confirm("Overwrite the existing file?", on_confirm, on_cancel, kind="warning", width=320)
```

```python
ui.confirm("Push 3 commits to main?", on_push, on_cancel, confirm_label="Push",
           cancel_label="Not now", width=320)
```

## Events

Confirm has **2 events 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_confirm` | `()` | The confirm button. |
| `on_cancel` | `()` | Cancel, 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 Files(pz.App):
    def __init__(self):
        self.files = ["notes.py", "todo.txt"]
        self.deleting = "notes.py"       # the file waiting for an answer

    def delete(self):                    # on_confirm
        self.files.remove(self.deleting)
        self.deleting = None

    def view(self):
        dialog = None
        if self.deleting:
            dialog = ui.confirm(f"Delete {self.deleting}?", self.delete,
                                lambda: setattr(self, "deleting", None),   # on_cancel
                                title="Delete file", danger=True, confirm_label="Delete")
        return ui.dialog_layer(main_view(), dialog)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Delete:
    name: str

class Files(pz.App):
    files = ["notes.py", "todo.txt"]
    deleting = "notes.py"

    def update(self, message):
        match message:
            case Delete(name):
                self.files = [f for f in self.files if f != name]
                self.deleting = None
            case "cancel":
                self.deleting = None

    def view(self):
        dialog = ui.confirm(f"Delete {self.deleting}?", Delete(self.deleting), "cancel", danger=True) \
            if self.deleting else None
        return ui.dialog_layer(main_view(), dialog)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    files: tuple = ("notes.py", "todo.txt")
    deleting: str = "notes.py"

def view(state, set_state):
    if not state.deleting:
        return main_view()
    keep = tuple(f for f in state.files if f != state.deleting)
    return ui.dialog_layer(main_view(), ui.confirm(
        f"Delete {state.deleting}?", lambda: set_state(files=keep, deleting=""),
        lambda: set_state(deleting=""), danger=True))
```

## Properties and API

### `ui.confirm`

```python
confirm(
    message,
    on_confirm,
    on_cancel,
    *,
    title=None,
    confirm_label=None,
    cancel_label=None,
    danger=False,
    kind=None,
    width=420,
    theme=None,
)
```

Asks a yes/no question.

| Property | Default | Description |
| --- | --- | --- |
| `message` | *required* | Text or widget. |
| `title` | `None` | Title. |
| `confirm_label` | `None` | Confirm button text. |
| `cancel_label` | `None` | Cancel button text. |
| `danger` | `False` | Paints the confirm button red and labels it Delete. |
| `kind` | `None` | Adds an icon, as in `alert`. |
| `width` | `420` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_confirm`, `on_cancel` ([events](#events)).

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

## Related

- [Alert](https://albakore.github.io/pyzed/library/alert/): A message with one button.
- [Choice](https://albakore.github.io/pyzed/library/choice/): A message with your own buttons.
- [Unsaved changes](https://albakore.github.io/pyzed/library/unsaved-changes/): “Save your changes before closing?”
