Prompt the user for a single line of text.
Calls on_ok with the user's input when the user clicks OK.
Source code in src/awesome_os/frontend/dialogs.py
| def prompt_text(
*,
parent: ttk.TTkWidget,
title: str,
label: str,
initial: str,
on_ok: Callable[[str], None],
) -> None:
"""Prompt the user for a single line of text.
Calls `on_ok` with the user's input when the user clicks **OK**.
"""
dialog = ttk.TTkWindow(
parent=parent,
title=TTkString(f" {title} ", ttk.TTkColor.fg("#00aaff") + ttk.TTkColor.bg("#003355")),
size=(80, 18),
border=True,
layout=ttk.TTkVBoxLayout(),
)
_center_over_parent(widget=dialog, parent=parent)
ttk.TTkLabel(parent=dialog, text=label, maxHeight=2)
edit = ttk.TTkLineEdit(parent=dialog)
edit.setText(initial)
buttons = ttk.TTkFrame(parent=dialog, layout=ttk.TTkHBoxLayout())
ok_btn = ttk.TTkButton(parent=buttons, text=TTkString("OK"))
cancel_btn = ttk.TTkButton(parent=buttons, text=TTkString("Cancel"))
def _accept() -> None:
value = edit.text()
dialog.close()
# TermTk returns a TTkString-like object; normalize it to a plain str for callers.
on_ok(str(value))
def _cancel() -> None:
dialog.close()
ok_btn.clicked.connect(_accept)
cancel_btn.clicked.connect(_cancel)
|