PyHML Language Reference
PyHML (PyHMI Markup Language) is a declarative UI description format inspired by Qt QML. It uses a YAML-compatible, indented syntax to describe UI hierarchies. PyHML files are parsed at runtime, constructing a widget tree that the engine renders on the active display backend.
Basic Syntax
PyHML uses indentation to define parent-child relationships. Each line declares a widget or component, followed by its properties:
Window:
width: 320
height: 240
title: "My App"
background_color: [30, 30, 30, 255]
Label:
text: "Hello, World!"
text_color: "#FFFFFF"
font_size: 16
The Window is the root element. Label is a child of Window, indicated by indentation. Properties are assigned with key: value syntax using snake_case names (font_size, text_color, background_color).
Component Hierarchy
Nesting is defined by indentation. Each level of indentation creates a parent-child relationship:
Window:
layout: Column
padding: 10
Label:
text: "Username"
TextField:
id: username
CheckBox:
text: "Remember me"
Button:
text: "Login"
onClicked: print("login", username.text)
In this example, Label, TextField, CheckBox, and Button are all children of Window. The Column layout arranges them vertically. Handler code is Python evaluated in a sandboxed context (see Signal Handlers).
Property Assignment
Properties are assigned using YAML-style syntax. Supported types:
| Type | Syntax | Example |
|---|---|---|
| String | Quoted or unquoted | text: "Hello" |
| Integer | Plain number | width: 320 |
| Float | Decimal number | opacity: 0.5 |
| Boolean | true / false | visible: true |
| Color | Hex string | color: "#FF5733" |
| List | Inline or block | values: [1, 2, 3] |
| Object | Nested mapping | font: {name: "Arial", size: 14} |
IDs and References
Widgets can be assigned an id for cross-component referencing:
Slider:
id: volume
min: 0
max: 100
value: 50
Label:
text: "Volume: {{ volume.value }}"
The id creates a reference that can be used in data bindings, signal handlers, and Python code.
Data Binding
PyHML binds properties to expressions with {{ }}. Any widget with an id (and every widget with a binding - ids are auto-generated when missing) is addressable by id.property:
Slider:
id: tempSlider
min: 0
max: 100
value: 22
Label:
text: "{{ tempSlider.value }} C"
Label:
text: "{{ 'OK' if tempSlider.value < 30 else 'Hot' }}"
Bindings update the target whenever the source changes. Expressions are Python (conditional expressions, arithmetic and string formatting are supported). Two-way sync between a widget and a Python observable is done through the binding API in code (engine.binding_engine.bind(...)); in PyHML the {{ }} form is one-way.
Signal Handlers
Signal handlers use the on<SignalName> prefix. The value is Python - a single expression or full statements:
Button:
text: "Submit"
onClicked: print("submitted")
Slider:
id: brightness
onValueChanged: print("level", value)
TextField:
id: search
onFocusIn: vkbDialog.target = self; vkbDialog.visible = true
Inside a handler, bare names resolve against the widget's own state (value, checked, text), and every registered id of the scene is addressable (QML id-reference semantics). Available builtins are whitelisted: print, min, max, abs, round, len, range, int, str, float, bool, enumerate, zip, sorted, sum, pow, ord, chr.
Common signals: Clicked, ValueChanged, TextChanged, Pressed, Released, FocusIn, FocusOut. A TextField emits FocusIn on every tap - the standard pattern for opening the virtual keyboard.
Layouts
PyHMI supports two layout engines:
Flex Layout
Row and column layouts with alignment, spacing, and stretch factors.
Window:
layout: Column
spacing: 10
alignment: center
Label:
text: "Title"
Button:
text: "OK"
stretch: 1
Button:
text: "Cancel"
stretch: 1
Grid Layout
Row and column grid with span, alignment, and flexible sizing.
Window:
layout: Grid
Label:
text: "Name"
TextField:
id: name
Label:
text: "Age"
TextField:
id: age
layout: Grid creates a default grid. Fine-grained control (columns, rows, column_spacing, row_spacing, spans) is set on the GridLayout object from Python.
Animations
Animations are driven from Python, not declared in PyHML. Use the animation API on any parsed scene:
from pyhmi import Easing
# after parsing the scene:
engine.animation_engine.animate(
my_button,
duration=0.3,
easing=Easing.EASE_IN_OUT,
opacity=0.5,
)
Supported easing curves: linear, easeIn, easeOut, easeInOut, bounce. Bindings declared in PyHML ({{ }}) update automatically when their source changes.
Styling
Widgets take style properties directly in PyHML:
Button:
text: "Submit"
background_color: "#4A90D9"
text_color: "#FFFFFF"
border_radius: 8
font_size: 14
Reusable styling is done with StyleTemplate registered on the engine (Python API) or with .pyhmt theme files applied via apply_theme_to_tree - see Theming.
Custom Elements
Custom widgets defined in Python can be used in PyHML:
Window:
MyGauge:
value: 75
maxValue: 100
color: "#4A90D9"
onValueChanged: update_label(value)
Custom elements support property binding, event handling, and style templates. They can be nested within other custom elements.
QML Import
PyHMI imports Qt QML designs by translating them to PyHMI widget trees:
from pyhmi import Engine, Sdl2Display
from pyhmi.parsers.qml import QMLImporter
display = Sdl2Display(width=320, height=240)
engine = Engine(display)
importer = QMLImporter(engine)
importer.import_qml_file("design.qml") # or import_qml(qml_content_string)
engine.run()
Supported QML features: component hierarchy, property assignment, signal handlers, ids, and simple bindings.
Scene Management
Scenes are self-contained PyHML files (or Scene/WidgetBase objects). The SceneManager loads them and manages navigation with data segue:
from pyhmi import Engine, Sdl2Display
from pyhmi.core.scene import SceneManager
display = Sdl2Display(width=320, height=240)
engine = Engine(display)
scenes = SceneManager(engine)
scenes.load("login.pyhml")
# forward navigation with data
scenes.push("dashboard.pyhml", segue={"user": "manu"})
# back navigation - the popped scene returns its data
result = scenes.pop()
load() accepts a PyHML file path, a Scene, or a WidgetBase, plus optional segue data and a transition config (type, duration, direction). Scenes receive segue data in enter(segue) and return data in exit().