UI API
Overview
MatrixOS.UI exposes common Matrix OS UI utilities and components to Python.
Selector Constants
Direction constants:
DIRECTION_RIGHT_THEN_DOWNDIRECTION_DOWN_THEN_RIGHTDIRECTION_LEFT_THEN_DOWNDIRECTION_DOWN_THEN_LEFTDIRECTION_UP_THEN_RIGHTDIRECTION_RIGHT_THEN_UPDIRECTION_UP_THEN_LEFTDIRECTION_LEFT_THEN_UP
Lit mode constants:
LIT_EQUALLIT_LESS_EQUAL_THANLIT_GREATER_EQUAL_THANLIT_ALWAYS
Utility Methods
MatrixOS.UI.text_scroll
text_scroll(text: str, color: ColorLike, speed: int = 10, loop: bool = False) -> None
Shows scrolling text.
Parameters:
text: Text to show.color: Packed RGB integer, RGB tuple, or RGBW tuple.speed: Scroll speed.loop: Whether the scroll should loop.
MatrixOS.UI.color_picker
color_picker(color: ColorLike, shade: bool = True) -> int | None
Opens the Matrix OS color picker.
Parameters:
color: Initial color.shade: Whether to show shade controls.
Returns:
int | None: Packed RGB color when confirmed, orNonewhen cancelled.
MatrixOS.UI.number_selector
number_selector(value: int, color: ColorLike, name: str, lower_limit: int = INT_MIN, upper_limit: int = INT_MAX) -> int
Opens a number selector.
Parameters:
value: Initial value.color: UI color.name: Label shown by the selector.lower_limit: Minimum value.upper_limit: Maximum value.
Returns:
int: Selected value.
MatrixOS.UI.UI
MatrixOS.UI.UI(name: str = "", color: ColorLike = MatrixOS.Color(255, 255, 255), new_layer: bool = True)
Creates a UI container.
Parameters:
name: UI name.color: UI accent color.new_layer: Whether the UI should create a new LED layer.
ui.start
start() -> None
Starts the UI and returns after the UI exits.
ui.exit
exit() -> None
Requests the UI to exit.
ui.close
close() -> bool
Releases the native UI object and attached components.
Returns:
bool:Truewhen closed.
ui.set_name
set_name(name: str) -> None
Sets the UI name.
ui.set_color
set_color(color: ColorLike) -> None
Sets the UI accent color.
ui.set_new_layer
set_new_layer(create: bool) -> None
Controls whether the UI creates a new LED layer.
ui.allow_exit
allow_exit(allow: bool) -> None
Controls whether the user can exit the UI.
ui.set_fps
set_fps(fps: int) -> None
Sets the UI frame rate.
ui.add
add(component, position: tuple[int, int]) -> None
Adds a component at a position.
Parameters:
component:Button,Selector,Number,Toggle, orCustomComponent.position: Component origin(x, y).
ui.clear
clear() -> None
Removes all components from the UI.
ui.set_setup_func
set_setup_func(callback) -> None
Sets a callback called when the UI starts. The callback receives no arguments.
ui.set_loop_func
set_loop_func(callback) -> None
Sets a UI loop callback. The callback receives no arguments.
ui.set_global_loop_func
set_global_loop_func(callback) -> None
Sets a global loop callback. The callback receives no arguments.
ui.set_pre_render_func
set_pre_render_func(callback) -> None
Sets a callback called before rendering. The callback receives no arguments.
ui.set_post_render_func
set_post_render_func(callback) -> None
Sets a callback called after rendering. The callback receives no arguments.
ui.set_end_func
set_end_func(callback) -> None
Sets a callback called when the UI ends. The callback receives no arguments.
ui.set_input_handler
set_input_handler(callback) -> None
Sets a callback for input events.
Callback:
- Called as
callback(event). - Return a truthy value to consume the event.
Simple Settings UI
import MatrixOS
KEY_SPEED = "HelloPython speed"
# Create a modal settings UI on its own LED layer.
settings = MatrixOS.UI.UI("Settings", (0, 255, 255), True)
# An 8x1 selector gives one row of speed choices.
selector = MatrixOS.UI.Selector((8, 1), 8)
selector.set_name("Speed")
selector.set_color((0, 255, 255))
# Restore the last saved value, or use 3 the first time.
selector.set_value(MatrixOS.NVS.get(KEY_SPEED, 3))
def save_speed(value):
# Persist changes immediately when the selector changes.
MatrixOS.NVS.set(KEY_SPEED, value)
selector.on_change(save_speed)
# Put the selector on the bottom row and start the UI.
settings.add(selector, (0, 7))
settings.start()
Shared Component Methods
Button, Selector, Number, Toggle, and CustomComponent support these methods.
component.close
close() -> bool
Closes a component that is not currently attached to a UI.
Returns:
bool:Truewhen closed. ReturnsFalseif the component is still attached.
component.set_enabled
set_enabled(enabled: bool) -> None
Enables or disables the component.
component.set_enable_func
set_enable_func(callback) -> None
Sets a callback that controls whether the component is enabled.
Callback:
- Called as
callback(). - Return truthy to enable the component.
MatrixOS.UI.Button
MatrixOS.UI.Button(name: str = "", color: ColorLike = MatrixOS.Color(255, 255, 255))
Creates a button component.
button.set_name
set_name(name: str) -> None
Sets the button name.
button.set_color
set_color(color: ColorLike) -> None
Sets the button color.
button.set_size
set_size(size: tuple[int, int]) -> None
Sets the button size.
button.set_color_func
set_color_func(callback) -> None
Sets a dynamic color callback.
Callback: Called as callback() and should return a color.
button.on_press
on_press(callback) -> None
Sets the press callback.
Callback: Called as callback().
button.on_hold
on_hold(callback) -> None
Sets the hold callback.
Callback: Called as callback().
MatrixOS.UI.Selector
MatrixOS.UI.Selector(dimension: tuple[int, int] = (1, 1), count: int = 1)
Creates a selector component.
selector.set_value
set_value(value: int) -> None
Sets the selected value.