Elección
Un mensaje con tus propios botones: options son pares (etiqueta, on_press) y el último es el principal.
ui.choice · Categoría: Diálogos · Referencia completa →


ui.dialog_layer(main_view(), ui.choice( "notes.py already exists in this folder.", [("Keep both", on_keep), ("Skip", on_skip), ("Replace", on_replace)], on_cancel, title="File exists", width=420,))Cuándo usarlo
Sección titulada «Cuándo usarlo»- Cuando una pregunta tiene más de dos respuestas (Conservar ambos / Reemplazar / Omitir).
from pyzed import ui
ui.dialog_layer(main_view(), ui.choice( "notes.py already exists in this folder.", [("Keep both", on_keep), ("Skip", on_skip), ("Replace", on_replace)], on_cancel, title="File exists", width=420,))Variantes
Sección titulada «Variantes»Opciones
Sección titulada «Opciones»

Código
ui.choice("Open the project in…", [("This window", on_here), ("New window", on_new)], on_cancel, width=360)

Código
ui.choice("notes.py already exists.", [("Keep both", on_keep), ("Skip", on_skip), ("Replace", on_replace)], on_cancel, title="File exists", width=400)Eventos
Sección titulada «Eventos»Elección tiene 1 evento propios, en la tabla de abajo. Como todo widget, también tiene los 18 eventos comunes (click, double_click, right_click, mouse_enter, drag, key_down, resize…), que se conectan con .on(...): mira Eventos y Eventos de cada componente.
| Evento | El handler recibe | Cuándo |
|---|---|---|
on_cancel |
() |
El icono de cerrar. |
El handler recibe es lo que Pyzed le pasa a tu función: () es nada. Cualquier handler también puede ser un mensaje (un valor que no se puede llamar), que va a update().
Manejar los eventos
Sección titulada «Manejar los eventos»Los mismos eventos escritos de tres formas. Imperativa: métodos de la app que cambian self. Declarativa: los handlers producen mensajes y un único update() decide. Funcional: una función pura dibuja un estado inmutable y pide cambios con set_state(...). Mira Imperativo, declarativo y funcional.
class Copy(pz.App): def __init__(self): self.asking, self.answer = True, None
def answer_with(self, what): self.asking, self.answer = False, what
def view(self): dialog = ui.choice("notes.py already exists in this folder.", [("Keep both", lambda: self.answer_with("both")), # one handler per option ("Replace", lambda: self.answer_with("replace"))], lambda: setattr(self, "asking", False), # on_cancel: the ✕ title="File exists") return ui.dialog_layer(main_view(), dialog if self.asking else None)from dataclasses import dataclass
@dataclassclass Answer: what: str
class Copy(pz.App): asking = True
def update(self, message): match message: case Answer(what): self.asking = False print("chose", what) case "cancel": self.asking = False
def view(self): dialog = ui.choice("notes.py already exists.", [("Keep both", Answer("both")), ("Replace", Answer("replace"))], "cancel") return ui.dialog_layer(main_view(), dialog if self.asking else None)from dataclasses import dataclass
@dataclass(frozen=True)class State: answer: str = ""
def view(state, set_state): if state.answer: return ui.label(f"You chose: {state.answer}") return ui.dialog_layer(main_view(), ui.choice( "notes.py already exists.", [(label, lambda a=answer: set_state(answer=a)) for label, answer in (("Keep both", "both"), ("Replace", "replace"))], lambda: set_state(answer="cancel")))Propiedades y API
Sección titulada «Propiedades y API»ui.choice
Sección titulada «ui.choice»choice(message, options, on_cancel, *, title=None, width=440, theme=None)Un mensaje con tus propios botones.
| Propiedad | Por defecto | Descripción |
|---|---|---|
message |
obligatorio | Cualquier valor de Python, o un callable. |
options |
obligatorio | Pares (texto, on_press); el último es el botón principal. |
title |
None |
Título. |
width |
440 |
Ancho: píxeles, "fill", "shrink" o "fill:N" (proporción). |
theme |
None |
Tema a usar en lugar del actual (pyzed.theme.current()). |
Eventos: on_cancel (eventos).