Confirm
Asks a yes/no question. danger=True paints the confirm button red; confirm_label and cancel_label rename the buttons.
ui.confirm · Category: Dialogs · Full reference →


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,))When to use
Section titled “When to use”- Before destructive or irreversible actions: delete, overwrite, discard.
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
Section titled “Variants”

Code
ui.confirm("Close all tabs?", on_confirm, on_cancel, title="Close tabs", width=320)

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

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

confirm_label=, cancel_label=Code
ui.confirm("Push 3 commits to main?", on_push, on_cancel, confirm_label="Push", cancel_label="Not now", width=320)Events
Section titled “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 and Events of every component.
| 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
Section titled “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.
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)from dataclasses import dataclass
@dataclassclass 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)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
Section titled “Properties and API”ui.confirm
Section titled “ui.confirm”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).