libui.declarative — Declarative API¶
Declarative UI layer for python-libui-ng.
- class libui.declarative.State(initial: T)¶
Bases:
Generic[T]Reactive container. Notifies subscribers on change.
Usage:
value = State(0) value.subscribe(lambda: print(value.value)) value.value = 42 # prints 42
- property value: T¶
- get() T¶
- class libui.declarative.Computed(source: State | Computed, fn: Callable)¶
Bases:
Generic[T]Readonly derived state. Updates automatically when source changes.
- property value: T¶
- get() T¶
- class libui.declarative.ListState(initial: list[T] | None = None)¶
Bases:
Generic[T]Observable list for tables. Notifies on insert/delete/change.
- class libui.declarative.BuildContext(window=None)¶
Bases:
objectCarries shared state during the build phase.
- class libui.declarative.Node(**kwargs)¶
Bases:
objectBase descriptor for a UI element. Lightweight — does not create widgets.
Subclasses override
create_widget()and optionallyattach_children().- create_widget(ctx: BuildContext) Any¶
- build(ctx: BuildContext) Any¶
Materialise the widget tree rooted at this node.
- attach_children(widget: Any, ctx: BuildContext) None¶
Override to add children to the widget.
- class libui.declarative.VBox(*children: Node, padded: bool = True)¶
Bases:
NodeVertical box container.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- class libui.declarative.HBox(*children: Node, padded: bool = True)¶
Bases:
NodeHorizontal box container.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- class libui.declarative.Group(title: str | State[str], child: Node, margined: bool = True)¶
Bases:
NodeLabeled group container with a single child.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- class libui.declarative.Form(*rows: tuple, padded: bool = True)¶
Bases:
NodeTwo-column label-control form.
Children are
(label, node)tuples or(label, node, stretchy)triples.- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- class libui.declarative.Tab(*pages: tuple[str, Node], margined: bool = True)¶
Bases:
NodeTabbed container. Children are
("Page Name", node)tuples.- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- class libui.declarative.Grid(*cells: GridCell, padded: bool = True)¶
Bases:
NodeGrid layout container.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- class libui.declarative.GridCell(child: Node, left: int, top: int, xspan: int = 1, yspan: int = 1, hexpand: bool = False, halign=Align.FILL, vexpand: bool = False, valign=Align.FILL)¶
Bases:
objectPlacement descriptor for a child in a Grid.
- class libui.declarative.Label(text: str | State[str] | Computed[str] = '')¶
Bases:
NodeStatic text label.
- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- class libui.declarative.Button(text: str = '', on_clicked: Callable | None = None)¶
Bases:
NodeClickable button.
- create_widget(ctx)¶
- class libui.declarative.Entry(text: State[str] | str | None = None, type: str = 'normal', read_only: State[bool] | bool = False, on_changed: Callable | None = None)¶
Bases:
NodeSingle-line text entry with optional two-way binding.
- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.Checkbox(text: str = '', checked: State[bool] | bool = False, on_toggled: Callable | None = None)¶
Bases:
NodeCheckbox with optional two-way binding for
checked.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.Slider(min: int = 0, max: int = 100, value: State[int] | int = 0, has_tooltip: bool = True, on_changed: Callable | None = None)¶
Bases:
NodeHorizontal slider with optional two-way
valuebinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.Spinbox(min: int = 0, max: int = 100, value: State[int] | int = 0, on_changed: Callable | None = None)¶
Bases:
NodeNumeric spinbox with optional two-way
valuebinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.ProgressBar(value: State[int] | Computed[int] | int = 0)¶
Bases:
NodeProgress bar (one-way binding only).
- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- class libui.declarative.Combobox(items: Sequence[str] = (), selected: State[int] | int = 0, on_selected: Callable | None = None)¶
Bases:
NodeDrop-down combobox with optional two-way
selectedbinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.RadioButtons(items: Sequence[str] = (), selected: State[int] | int = -1, on_selected: Callable | None = None)¶
Bases:
NodeRadio button group with optional two-way
selectedbinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.EditableCombobox(items: Sequence[str] = (), text: State[str] | str = '', on_changed: Callable | None = None)¶
Bases:
NodeEditable combobox with optional two-way
textbinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.MultilineEntry(text: State[str] | str = '', wrapping: bool = True, read_only: State[bool] | bool = False, on_changed: Callable | None = None)¶
Bases:
NodeMulti-line text entry with optional two-way
textbinding.- create_widget(ctx)¶
- bind_props(widget)¶
Bind State/Computed props to widget attributes.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.ColorButton(on_changed: Callable | None = None)¶
Bases:
NodeColor picker button.
- create_widget(ctx)¶
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.FontButton(on_changed: Callable | None = None)¶
Bases:
NodeFont picker button.
- create_widget(ctx)¶
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.DateTimePicker(type: str = 'datetime', on_changed: Callable | None = None)¶
Bases:
NodeDate/time picker.
- create_widget(ctx)¶
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.Separator(vertical: bool = False)¶
Bases:
NodeVisual separator line.
- create_widget(ctx)¶
- class libui.declarative.DrawArea(on_draw: Callable | None = None, on_mouse_event: Callable | None = None, on_mouse_crossed: Callable | None = None, on_drag_broken: Callable | None = None, on_key_event: Callable | None = None)¶
Bases:
NodeCustom drawing area with optional event callbacks.
- Callbacks:
- on_draw(ctx, area_w, area_h, clip_x, clip_y, clip_w, clip_h):
Called to paint the area. Must be synchronous.
- on_mouse_event(event_dict):
Mouse button/move. Keys: x, y, area_width, area_height, down, up, count, modifiers, held. May be async.
- on_mouse_crossed(left: bool):
Mouse entered (False) or left (True) the area. May be async.
- on_drag_broken():
OS interrupted a drag. May be async.
- on_key_event(event_dict) -> bool:
Key press/release. Keys: key, ext_key, modifier, modifiers, up. Return True to consume. Must be synchronous.
- create_widget(ctx)¶
- class libui.declarative.ScrollingDrawArea(on_draw: Callable | None = None, width: int = 1000, height: int = 1000, on_mouse_event: Callable | None = None, on_mouse_crossed: Callable | None = None, on_drag_broken: Callable | None = None, on_key_event: Callable | None = None)¶
Bases:
NodeScrollable custom drawing area with a fixed content size.
Same callbacks as DrawArea, plus width/height for the scrollable region.
- create_widget(ctx)¶
- class libui.declarative.DataTable(data: ListState, *columns: _ColumnDescriptor, on_row_clicked: Callable | None = None, on_row_double_clicked: Callable | None = None, on_header_clicked: Callable | None = None, on_selection_changed: Callable | None = None)¶
Bases:
NodeDeclarative table backed by a
ListState.Each row in the ListState is a dict. Column descriptors define which keys to display and how.
- create_widget(ctx)¶
- class libui.declarative.TextColumn(name: str, key: str, editable: bool = False, color_col: int = -1, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.CheckboxColumn(name: str, key: str, editable: bool = True, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.CheckboxTextColumn(name: str, checkbox_key: str, text_key: str, checkbox_editable: bool = True, text_editable: bool = False, text_color_col: int = -1, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.ProgressColumn(name: str, key: str, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.ButtonColumn(name: str, text_key: str, on_click: Callable | None = None, clickable: bool = True, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.ImageTextColumn(name: str, image_key: str, text_key: str, editable: bool = False, color_col: int = -1, width: int = -1)¶
Bases:
_ColumnDescriptor
- class libui.declarative.App(window: Window | None = None, menus: list[MenuDef] | None = None)¶
Bases:
objectApplication root. Manages menus and a single window.
- build(window: Window | None = None, menus: list[MenuDef] | None = None) None¶
Materialise the full widget tree: menus first, then window.
Optional window and menus args override values set in
__init__.
- property window¶
The underlying core Window widget (available after build).
- class libui.declarative.Window(title: str = 'Application', width: int = 800, height: int = 600, child: Node | None = None, has_menubar: bool = False, margined: bool = True, on_closing: Callable | None = None)¶
Bases:
NodeTop-level window descriptor.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.MenuDef(title: str, *items: _MenuEntry)¶
Bases:
objectDefinition for a top-level menu.
- class libui.declarative.MenuItem(name: str, on_click: Callable | None = None)¶
Bases:
_MenuEntryA regular clickable menu item.
- class libui.declarative.CheckMenuItem(name: str, checked: State[bool] | None = None, on_click: Callable | None = None)¶
Bases:
_MenuEntryA checkable menu item with optional State binding.
- class libui.declarative.MenuSeparator¶
Bases:
_MenuEntryA separator line in a menu.
- class libui.declarative.QuitItem¶
Bases:
_MenuEntryPlatform Quit menu item.
- class libui.declarative.PreferencesItem¶
Bases:
_MenuEntryPlatform Preferences menu item.
- class libui.declarative.AboutItem¶
Bases:
_MenuEntryPlatform About menu item.
App & Window¶
App root, Window, and Menu descriptors for the declarative UI layer.
- class libui.declarative.app.MenuItem(name: str, on_click: Callable | None = None)¶
Bases:
_MenuEntryA regular clickable menu item.
- class libui.declarative.app.CheckMenuItem(name: str, checked: State[bool] | None = None, on_click: Callable | None = None)¶
Bases:
_MenuEntryA checkable menu item with optional State binding.
- class libui.declarative.app.MenuSeparator¶
Bases:
_MenuEntryA separator line in a menu.
- class libui.declarative.app.QuitItem¶
Bases:
_MenuEntryPlatform Quit menu item.
- class libui.declarative.app.PreferencesItem¶
Bases:
_MenuEntryPlatform Preferences menu item.
- class libui.declarative.app.AboutItem¶
Bases:
_MenuEntryPlatform About menu item.
- class libui.declarative.app.MenuDef(title: str, *items: _MenuEntry)¶
Bases:
objectDefinition for a top-level menu.
- class libui.declarative.app.Window(title: str = 'Application', width: int = 800, height: int = 600, child: Node | None = None, has_menubar: bool = False, margined: bool = True, on_closing: Callable | None = None)¶
Bases:
NodeTop-level window descriptor.
- create_widget(ctx)¶
- attach_children(widget, ctx)¶
Override to add children to the widget.
- attach_callbacks(widget)¶
Register event callbacks on the widget.
- class libui.declarative.app.App(window: Window | None = None, menus: list[MenuDef] | None = None)¶
Bases:
objectApplication root. Manages menus and a single window.
- build(window: Window | None = None, menus: list[MenuDef] | None = None) None¶
Materialise the full widget tree: menus first, then window.
Optional window and menus args override values set in
__init__.
- property window¶
The underlying core Window widget (available after build).