Unsaved changes
“Save your changes to name before closing?” with Don’t save, Cancel and Save.
ui.unsaved_changes · Category: Dialogs · Full reference →


ui.dialog_layer(main_view(), ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel))When to use
Section titled “When to use”- When closing a tab or a window with unsaved edits.
from pyzed import ui
ui.dialog_layer(main_view(), ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel))Variants
Section titled “Variants”Options
Section titled “Options”

Code
ui.unsaved_changes("notes.py", on_save, on_discard, on_cancel)

width=360Code
ui.unsaved_changes("report.pzform", on_save, on_discard, on_cancel, width=360)Events
Section titled “Events”Unsaved changes has 3 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_save |
() |
Save. |
on_discard |
() |
Don’t save. |
on_cancel |
() |
Cancel. |
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 Notes(pz.App): def __init__(self): self.closing = True
def save(self): self.closing = False # write the file, then close
def view(self): dialog = ui.unsaved_changes("notes.py", self.save, lambda: setattr(self, "closing", False), # on_discard lambda: setattr(self, "closing", False)) # on_cancel return ui.dialog_layer(main_view(), dialog if self.closing else None)class Notes(pz.App): closing = True
def update(self, message): match message: case "save" | "discard": self.closing = False case "cancel": self.closing = False
def view(self): dialog = ui.unsaved_changes("notes.py", "save", "discard", "cancel") return ui.dialog_layer(main_view(), dialog if self.closing else None)from dataclasses import dataclass
@dataclass(frozen=True)class State: closing: bool = True saved: bool = False
def view(state, set_state): dialog = ui.unsaved_changes("notes.py", lambda: set_state(saved=True, closing=False), lambda: set_state(closing=False), lambda: set_state(closing=False)) return ui.dialog_layer(main_view(), dialog if state.closing else None)Properties and API
Section titled “Properties and API”ui.unsaved_changes
Section titled “ui.unsaved_changes”unsaved_changes(name, on_save, on_discard, on_cancel, *, width=440, theme=None)“Save your changes to name before closing?” with Don’t save, Cancel and Save.
| Property | Default | Description |
|---|---|---|
name |
required | File or document name. |
width |
440 |
Width: pixels, "fill", "shrink" or "fill:N" (proportion). |
theme |
None |
Theme to use instead of the current one (pyzed.theme.current()). |
Events: on_save, on_discard, on_cancel (events).