PyHMI Tutorials

Step-by-step guides from beginner to advanced.

Table of Contents

  1. Hello World
  2. Your First PyHML File
  3. Data Binding
  4. Scene Navigation
  5. Custom Widgets
  6. Styling with Templates
  7. Animations
  8. Dialogs
  9. Building a Dashboard App
  10. Deploying to Embedded
  11. Theming your HMI

Tutorial 1: Hello World

Create a minimal PyHMI application with a single label.

Setup

# Install PyHMI
bash install.sh
source .venv/bin/activate

Code

from pyhmi import Engine, Sdl2Display, Label, Window, FlexLayout
from pyhmi.core.layout import FlexDirection, MainAxisAlignment, CrossAxisAlignment

# Create display and engine
display = Sdl2Display(width=320, height=240, title="Hello PyHMI")
engine = Engine(display)

# Create window
window = Window()
window.width = 320
window.height = 240
window.background_color = (30, 30, 30, 255)

# Set up flex layout (use the enums - plain strings do not match the layout comparisons)
layout = FlexLayout(
    direction=FlexDirection.COLUMN,
    main_alignment=MainAxisAlignment.CENTER,
    cross_alignment=CrossAxisAlignment.CENTER,
)
engine.set_layout(layout)

# Create label
label = Label()
label.text = "Hello, World!"
label.font_size = 20
label.text_color = (255, 255, 255)

# Add to window
window.add_child(label)

# Set as root
engine.set_root(window)
engine.tree.compute_layout()

# Run
engine.run()

Run

python hello.py

You should see a dark window with "Hello, World!" centered.

Tutorial 2: Your First PyHML File

PyHML is PyHMI's declarative UI language, inspired by Qt QML.

Create dashboard.pyhml

Window:
  width: 320
  height: 240
  background_color: [30, 30, 30, 255]
  layout: Column

  Label:
    text: "Temperature"
    font_size: 18
    text_color: [200, 200, 200]

  Slider:
    id: tempSlider
    min_value: 0
    max_value: 100
    value: 22
    width: 280
    height: 20

  Label:
    text: "{{ tempSlider.value }} degrees C"
    font_size: 24
    text_color: [0, 120, 215]

Load it

from pyhmi import Engine, Sdl2Display, PyHMLParser, FlexLayout
from pyhmi.core.layout import FlexDirection, MainAxisAlignment

display = Sdl2Display(width=320, height=240)
engine = Engine(display)
engine.set_layout(FlexLayout(direction=FlexDirection.COLUMN, main_alignment=MainAxisAlignment.CENTER, spacing=10))

parser = PyHMLParser(engine)
root = parser.parse_file("dashboard.pyhml")
engine.set_root(root)
engine.run()

Key Concepts

Tutorial 3: Data Binding

Data binding connects widget properties to data sources automatically.

One-Way Binding

from pyhmi import Engine, Sdl2Display, Label, Window, Observable, FlexLayout

display = Sdl2Display(width=320, height=240)
engine = Engine(display)

window = Window()
window.width = 320
window.height = 240

# Create observable
temperature = Observable(22, name="temp")
engine.binding_engine.define("temp", temperature)

# Create label with binding
label = Label()
label.id = "tempLabel"
label.text = "22 C"
label.font_size = 24
engine.binding_engine.bind(label, "text", "{{ temp }} C")
engine.binding_engine.apply_binding(label, "text", "{{ temp }} C")

# Set up auto-update
def update_label(new_value):
    engine.binding_engine.apply_binding(label, "text", "{{ temp }} C")

temperature.subscribe(update_label)

window.add_child(label)
engine.set_root(window)
engine.tree.compute_layout()

# Simulate temperature change
import time
for t in range(22, 31):
    temperature.value = t
    time.sleep(0.5)

engine.run()

Two-Way Binding with Slider

from pyhmi import Engine, Sdl2Display, Slider, Label, Window, FlexLayout
from pyhmi.core.layout import FlexDirection, MainAxisAlignment

display = Sdl2Display(width=320, height=240)
engine = Engine(display)

window = Window()
window.width = 320
window.height = 240

layout = FlexLayout(direction=FlexDirection.COLUMN, main_alignment=MainAxisAlignment.CENTER, spacing=15)
engine.set_layout(layout)

slider = Slider()
slider.id = "volume"
slider.min_value = 0
slider.max_value = 100
slider.value = 50
slider.width = 280
slider.height = 20

label = Label()
label.id = "volLabel"
label.text = "50%"
label.font_size = 20

# Bind label to slider
engine.binding_engine.define("volume", slider)
engine.binding_engine.bind(label, "text", "{{ volume.value }}%")
engine.binding_engine.apply_binding(label, "text", "{{ volume.value }}%")

# Update label on slider change
def on_change(event):
    engine.binding_engine.apply_binding(label, "text", "{{ volume.value }}%")

slider.on_signal("ValueChanged", on_change)

window.add_child(slider)
window.add_child(label)
engine.set_root(window)
engine.tree.compute_layout()
engine.run()

Tutorial 4: Scene Navigation

Multi-screen applications with scene management.

Basic Navigation

from pyhmi import (
    Engine, Sdl2Display, Window, Label, Button,
    FlexLayout, SceneManager,
)
from pyhmi.core.layout import FlexDirection, MainAxisAlignment

display = Sdl2Display(width=320, height=240)
engine = Engine(display)
manager = SceneManager(engine)

# Scene 1: Home
home = Window()
home.width = 320
home.height = 240
home.background_color = (30, 30, 30, 255)

home_label = Label()
home_label.text = "Home Screen"
home_label.font_size = 20

go_btn = Button()
go_btn.text = "Go to Settings"
go_btn.width = 160
go_btn.height = 32

def on_go(event):
    manager.push(settings, segue={"from": "home"})

go_btn.on_signal("Clicked", on_go)

home.add_child(home_label)
home.add_child(go_btn)

# Scene 2: Settings
settings = Window()
settings.width = 320
settings.height = 240
settings.background_color = (40, 40, 60, 255)

settings_label = Label()
settings_label.text = "Settings"
settings_label.font_size = 20

back_btn = Button()
back_btn.text = "Back"
back_btn.width = 120
back_btn.height = 32

def on_back(event):
    manager.pop()

back_btn.on_signal("Clicked", on_back)

settings.add_child(settings_label)
settings.add_child(back_btn)

# Set layout
layout = FlexLayout(direction=FlexDirection.COLUMN, main_alignment=MainAxisAlignment.CENTER, spacing=20)
engine.set_layout(layout)

# Load home scene
manager.load(home)
engine.tree.compute_layout()
engine.run()

With Transitions

# Fade transition
manager.push(settings, transition={"type": "fade", "duration": 0.5})

# Slide transition
manager.push(settings, transition={"type": "slide", "direction": "right", "duration": 0.3})

Data Segue

from pyhmi.core.scene import Scene

class ProfileScene(Scene):
    def enter(self, segue):
        super().enter(segue)
        # Receive data forward
        print("User:", segue.get("username"))

    def exit(self):
        # Return data backward to the previous scene
        return {"settings_updated": True}

# Pass data forward
scenes.push(ProfileScene(), segue={"username": "admin", "level": 5})

# Back navigation returns the popped scene's segue data
result = scenes.pop()
print(result)  # {"settings_updated": True}

Tutorial 5: Custom Widgets

Create reusable, domain-specific widgets.

Basic Custom Widget

from pyhmi import CustomElement, register_element

@register_element("TempGauge")
class TempGauge(CustomElement):
    value: float = 0.0
    unit: str = "C"
    min_value: float = -40.0
    max_value: float = 120.0

    def render(self, painter, x, y):
        # Background
        painter.draw_rect(x, y, self.width, self.height, (50, 50, 50, 255))

        # Value text
        text = f"{self.value:.1f}\u00b0{self.unit}"
        painter.draw_text(
            x + self.width // 2,
            y + self.height // 2,
            text, self.font_size, self.text_color, "center"
        )

        # Progress bar
        ratio = (self.value - self.min_value) / (self.max_value - self.min_value)
        bar_width = int(self.width * 0.8 * ratio)
        painter.draw_rect(
            x + self.width // 4, y + self.height - 10,
            bar_width, 6, (0, 120, 215, 255), fill=True
        )

Use in PyHML

Window:
  width: 320
  height: 240
  TempGauge:
    value: 22.5
    unit: "C"
    width: 280
    height: 60
    font_size: 24

Nested Custom Elements

@register_element("SensorCard")
class SensorCard(CustomElement):
    sensor_name: str = "Temperature"
    value: float = 0.0

    def render(self, painter, x, y):
        # Card background
        painter.draw_rect(x, y, self.width, self.height, (60, 60, 60, 255))

        # Render children (includes TempGauge)
        for child in self.children:
            child.render(painter, x + child.x, y + child.y)
SensorCard:
  sensor_name: "Kitchen"
  value: 22.5
  width: 300
  height: 100
  TempGauge:
    value: 22.5
    width: 280
    height: 60

Tutorial 6: Styling with Templates

Apply consistent styling across your application.

Define Templates

from pyhmi import StyleTemplate, Engine

dark_theme = StyleTemplate("dark")
dark_theme.set(
    background_color=(30, 30, 30, 255),
    text_color=(255, 255, 255),
    font_size=14,
    border_color=(80, 80, 80),
)

light_theme = StyleTemplate("light")
light_theme.set(
    background_color=(240, 240, 240, 255),
    text_color=(30, 30, 30),
    font_size=14,
    border_color=(180, 180, 180),
)

engine.register_template(dark_theme)
engine.register_template(light_theme)
engine.set_active_template("dark")

Switch Themes at Runtime

def on_theme_toggle(event):
    current = "dark" if engine._active_template == "light" else "light"
    engine.set_active_template(current)

Widget-Specific Overrides

Templates apply cascading: widget > class > theme. Explicit widget properties override templates.

button = Button()
button.text = "Important"
button.background_color = (255, 0, 0, 255)  # Overrides theme

Tutorial 7: Animations

Smooth transitions and dynamic effects.

Property Animation

from pyhmi import Button, Easing

btn = Button()
btn.text = "Slide Me"
btn.x = 0
btn.width = 120
btn.height = 40

# Animate x position to 200 over 1 second (durations are seconds)
engine.animation_engine.animate(
    btn,
    duration=1.0,
    easing=Easing.EASE_OUT,
    x=200,
)

Easing Curves

EasingEffect
LINEARConstant speed
EASE_INSlow start, fast end
EASE_OUTFast start, slow end
EASE_IN_OUTSlow both ends
BOUNCEBounce at end

Keyframe Animation

from pyhmi.core.animation import Keyframe

engine.animation_engine.animate(
    label,
    duration=2.0,
    keyframes=[
        Keyframe(time=0.0, values={"opacity": 0.0}),
        Keyframe(time=0.5, values={"opacity": 1.0}),
        Keyframe(time=1.0, values={"opacity": 0.0}),
    ],
)

Tutorial 8: Dialogs

Modal dialogs for user interaction.

Message Dialog

from pyhmi import DialogManager

dlg = DialogManager(engine)
dlg.show_message("Info", "Operation completed successfully!")

Confirmation Dialog

def on_confirm(result):
    if result.confirmed:
        delete_item()

dlg.show_confirmation("Delete", "Are you sure?", callback=on_confirm)

Input Dialog

def on_input(result):
    if result.confirmed:
        username = result.data.get("text", "")
        print(f"Username: {username}")

dlg.show_input("Login", "Enter username:", placeholder="admin", callback=on_input)

Tutorial 9: Building a Dashboard App

Complete multi-screen dashboard application.

from pyhmi import (
    Engine, Sdl2Display, Window, Label, Button, Slider, CheckBox,
    FlexLayout, SceneManager, DialogManager, StyleTemplate,
    LineChart, Gauge,
)
from pyhmi.core.layout import FlexDirection, MainAxisAlignment

# Setup
display = Sdl2Display(width=480, height=320, title="Dashboard")
engine = Engine(display)
manager = SceneManager(engine)
dlg = DialogManager(engine)

# Theme
dark = StyleTemplate("dark")
dark.set(background_color=(20, 20, 30, 255), text_color=(255, 255, 255), font_size=14)
engine.register_template(dark)
engine.set_active_template("dark")

# Home scene
home = Window()
home.width = 480
home.height = 320
home.background_color = (20, 20, 30, 255)

chart = LineChart()
chart.width = 400
chart.height = 200
chart.data = [10, 25, 30, 20, 45, 35, 50]

gauge = Gauge()
gauge.width = 120
gauge.height = 120
gauge.value = 75

settings_btn = Button()
settings_btn.text = "Settings"
settings_btn.width = 100
settings_btn.height = 32

def go_settings(event):
    manager.push(settings_scene, segue={"from": "home"})

settings_btn.on_signal("Clicked", go_settings)

# Layout and add children
layout = FlexLayout(direction=FlexDirection.COLUMN, main_alignment=MainAxisAlignment.CENTER, spacing=15)
engine.set_layout(layout)
home.add_child(chart)
home.add_child(gauge)
home.add_child(settings_btn)

# Settings scene
settings_scene = Window()
settings_scene.width = 480
settings_scene.height = 320
settings_scene.background_color = (30, 30, 50, 255)

alert_btn = Button()
alert_btn.text = "Show Alert"
alert_btn.width = 120
alert_btn.height = 32

def show_alert(event):
    dlg.show_alert("Warning", "This is a test alert!")

alert_btn.on_signal("Clicked", show_alert)

back_btn = Button()
back_btn.text = "Back"
back_btn.width = 100
back_btn.height = 32

def go_back(event):
    manager.pop()

back_btn.on_signal("Clicked", go_back)

settings_scene.add_child(alert_btn)
settings_scene.add_child(back_btn)

# Start
manager.load(home)
engine.tree.compute_layout()
engine.run()

Tutorial 10: Deploying to Embedded

Raspberry Pi (Framebuffer)

from pyhmi import Engine, PyHMLParser
from pyhmi.backends.framebuffer import FramebufferDisplay

display = FramebufferDisplay(path="/dev/fb0")
engine = Engine(display)

parser = PyHMLParser(engine)
root = parser.parse_file("dashboard.pyhml")
engine.set_root(root)
engine.run()

FramebufferDisplay is imported from its module (it is not a top-level export). For touch/keyboard/mouse input on Linux add the evdev backend: from pyhmi.backends.evdev_input import EvdevInput.

Raspberry Pi Pico / RP2350 (SPI LCD)

import board
import busio
from pyhmi import Engine, CircuitPythonDisplay

spi = busio.SPI(clock=board.GP10, MOSI=board.GP11)

lcd = CircuitPythonDisplay(
    width=240,
    height=320,
    controller="st7789",
    spi=spi,
    command=board.GP14,
    chip_select=board.GP13,
    reset=board.GP15,
)
engine = Engine(lcd)
engine.run()

Supported controllers: st7735, st7789, ili9341, hx8357, st7262. For the Waveshare RP2350-Touch-LCD-2.8 board there is a complete ready-to-build project with per-demo firmware and C-accelerated flushing in platform/waveshare-rp2350-touch-lcd-2.8/ (see Backends).

Memory Optimization for MCU

# Pre-decode bitmap glyph tables at boot, BEFORE the widget tree
# fragments the heap (MicroPython GC does not compact):
from pyhmi.core.font import prewarm_fonts
prewarm_fonts()  # sizes 10, 12, 14, 16, 20, 24

Further MCU guidance: use the pre-generated bitmap font (no Pillow on MCU), hoist every render-time buffer into a reused allocation secured at init, and enable partial-update row-diff flushing via the build-time PARTIAL_UPDATE=true flag in build_firmware.sh - it is a firmware build option, not a runtime property.

Tutorial 11: Theming your HMI

Switch the appearance of your whole HMI from a single .pyhmt file — no code changes.

1. Write a theme file

# mytheme.pyhmt
window:
  background: [15, 23, 42, 255]
widgets:
  Button:
    background_color: [37, 99, 235]
    text_color: [255, 255, 255]
global:
  accent: [37, 99, 235]
  font_size: 14

2. Apply it to a live scene

from pyhmi.cli.editor.theme import load_theme_file, apply_theme_to_tree

theme = load_theme_file("mytheme.pyhmt")
applied = apply_theme_to_tree(theme, window)  # window = your scene root

3. Edit it in the Scene Editor

The editor's ThemeBar (top toolbar) loads and saves themes. Type a name, press the load icon. New elements pick up the theme's colors, font size and corner radius automatically; the properties pane shows the active theme's globals when nothing is selected.

Expected result: the window background turns dark slate, buttons render blue with white text, and all labels use the theme font size.