slint
Slint-python (Alpha)
Slint is a UI toolkit that supports different programming languages. Slint-python is the integration with Python.
Warning: Alpha Slint-python is still in the very early stages of development: APIs will change and important features are still being developed, the project is overall incomplete.
You can track the overall progress for the Python integration by looking at python-labelled issues at https://github.com/slint-ui/slint/labels/a%3Alanguage-python .
Slint Language Manual
The Slint Language Documentation covers the Slint UI description language in detail.
Prerequisites
Installation
Slint can be installed with pip
or uv
from the Python Package Index:
uv add slint
The installation will use binaries provided vi macOS, Windows, and Linux for various architectures. If your target platform is not covered by binaries,
uv
will automatically build Slint from source. If that happens, you need common software development tools on your machine, as well as Rust.
Building from Source
Try it out
If you want to just play with this, you can try running our Python port of the printer demo:
cd demos/printerdemo/python
uv run main.py
Quick Start
- Create a new project with
uv init
. - Add Slint Python Package Index to your Python project:
uv add slint
- Create a file called
app-window.slint
:
import { Button, VerticalBox } from "std-widgets.slint";
export component AppWindow inherits Window {
in-out property<int> counter: 42;
callback request-increase-value();
VerticalBox {
Text {
text: "Counter: \{root.counter}";
}
Button {
text: "Increase value";
clicked => {
root.request-increase-value();
}
}
}
}
- Create a file called
main.py
:
import slint
# slint.loader will look in `sys.path` for `app-window.slint`.
class App(slint.loader.app_window.AppWindow):
@slint.callback
def request_increase_value(self):
self.counter = self.counter + 1
app = App()
app.run()
- Run it with
uv run main.py
API Overview
Instantiating a Component
The following example shows how to instantiate a Slint component in Python:
app.slint
export component MainWindow inherits Window {
callback clicked <=> i-touch-area.clicked;
in property <int> counter;
width: 400px;
height: 200px;
i-touch-area := TouchArea {}
}
The exported component is exposed as a Python class. To access this class, you have two options:
Call
slint.load_file("app.slint")
. The returned object is a namespace, that provides theMainWindow
class as well as any other explicitly exported component that inheritsWindow
:import slint components = slint.load_file("app.slint") main_window = components.MainWindow()
Use Slint's auto-loader, which lazily loads
.slint
files fromsys.path
:import slint # Look for for `app.slint` in `sys.path`: main_window = slint.loader.app.MainWindow()
Any attribute lookup in
slint.loader
is searched for insys.path
. If a directory with the name exists, it is returned as a loader object, and subsequent attribute lookups follow the same logic. If the name matches a file with the.slint
extension, it is automatically loaded withload_file
and the namespace is returned, which contains classes for each exported component that inheritsWindow
. If the file name contains a dash, likeapp-window.slint
, an attribute lookup forapp_window
will first try to locateapp_window.slint
and then fall back toapp-window.slint
.
Accessing Properties
Properties declared as out
or in-out
in .slint
files are visible as properties on the component instance.
main_window.counter = 42
print(main_window.counter)
Accessing Globals
Global Singletons are accessible in Python as properties in the component instance.
For example, this Slint code declares a PrinterJobQueue
singleton:
```slint,ignore
export global PrinterJobQueue {
in-out property
Access it as a property on the component instance by its name:
```python
print("job count:", instance.PrinterJobQueue.job_count)
Note: Global singletons are instantiated once per component. When declaring multiple components for export
to Python,
each instance will have their own instance of associated globals singletons.
Setting and Invoking Callbacks
Callbacks declared in .slint
files are visible as callable properties on the component instance. Invoke them
as function to invoke the callback, and assign Python callables to set the callback handler.
Callbacks in Slint can be defined using the callback
keyword and can be connected to a callback of an other component
using the <=>
syntax.
my-component.slint
export component MyComponent inherits Window {
callback clicked <=> i-touch-area.clicked;
width: 400px;
height: 200px;
i-touch-area := TouchArea {}
}
The callbacks in Slint are exposed as properties and that can be called as a function.
main.py
import slint
component = slint.loader.my_component.MyComponent()
# connect to a callback
def clicked():
print("hello")
component.clicked = clicked
// invoke a callback
component.clicked();
Another way to set callbacks is to sub-class and use the @slint.callback
decorator:
import slint
class Component(slint.loader.my_component.MyComponent):
@slint.callback
def clicked(self):
print("hello")
component = Component()
The @slint.callback()
decorator accepts a name
named argument, when the name of the method
does not match the name of the callback in the .slint
file. Similarly, a global_name
argument
can be used to bind a method to a callback in a global singleton.
Type Mappings
The types used for properties in the Slint Language each translate to specific types in Python. The follow table summarizes the entire mapping:
.slint Type |
Python Type | Notes |
---|---|---|
int |
int |
|
float |
float |
|
string |
str |
|
color |
slint.Color |
|
brush |
slint.Brush |
|
image |
slint.Image |
|
length |
float |
|
physical_length |
float |
|
duration |
float |
The number of milliseconds |
angle |
float |
The angle in degrees |
structure | dict /Struct |
When reading, structures are mapped to data classes, when writing dicts are also accepted. |
array | slint.Model |
Arrays and Models
Array properties can be set from Python by passing
subclasses of slint.Model
.
Use the slint.ListModel
class to construct a model from an iterable.
component.model = slint.ListModel([1, 2, 3]);
component.model.append(4)
del component.model[0]
When sub-classing slint.Model
, provide the following methods:
def row_count(self):
"""Return the number of rows in your model"""
def row_data(self, row):
"""Return data at specified row"""
def set_row_data(self, row, data):
"""For read-write models, store data in the given row. When done call set.notify_row_changed:"
..."""
self.notify_row_changed(row)
When adding/inserting rows, call notify_row_added(row, count)
on the super class. Similarly, removal
requires notifying Slint by calling notify_row_removed(row, count)
.
Structs
Structs declared in Slint and exposed to Python via export
are accessible in the namespace returned
when instantiating a component.
app.slint
export struct MyData {
name: string,
age: int
}
export component MainWindow inherits Window {
in-out property <MyData> data;
}
main.py
The exported MyData
struct can be constructed
import slint
# Look for for `app.slint` in `sys.path`:
main_window = slint.loader.app.MainWindow()
data = slint.loader.app.MyData(name = "Simon")
data.age = 10
main_window.data = data
Third-Party Licenses
For a list of the third-party licenses of all dependencies, see the separate Third-Party Licenses page.
1# Copyright © SixtyFPS GmbH <info@slint.dev> 2# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 3 4r""" 5.. include:: ../README.md 6""" 7 8import os 9import sys 10from . import slint as native 11import types 12import logging 13import copy 14import typing 15from typing import Any 16import pathlib 17from .models import ListModel, Model 18from .slint import Image, Color, Brush, Timer, TimerMode 19 20Struct = native.PyStruct 21 22 23class CompileError(Exception): 24 message: str 25 """The error message that produced this compile error.""" 26 27 diagnostics: list[native.PyDiagnostic] 28 """A list of detailed diagnostics that were produced as part of the compilation.""" 29 30 def __init__(self, message: str, diagnostics: list[native.PyDiagnostic]): 31 """@private""" 32 self.message = message 33 self.diagnostics = diagnostics 34 35 36class Component: 37 """Component is the base class for all instances of Slint components. Use the member functions to show or hide the 38 window, or spin the event loop.""" 39 40 __instance__: native.ComponentInstance 41 42 def show(self) -> None: 43 """Shows the window on the screen.""" 44 45 self.__instance__.show() 46 47 def hide(self) -> None: 48 """Hides the window from the screen.""" 49 50 self.__instance__.hide() 51 52 def run(self) -> None: 53 """Shows the window, runs the event loop, hides it when the loop is quit, and returns.""" 54 self.__instance__.run() 55 56 57def _normalize_prop(name: str) -> str: 58 return name.replace("-", "_") 59 60 61def _build_global_class(compdef: native.ComponentDefinition, global_name: str) -> Any: 62 properties_and_callbacks = {} 63 64 for prop_name in compdef.global_properties(global_name).keys(): 65 python_prop = _normalize_prop(prop_name) 66 if python_prop in properties_and_callbacks: 67 logging.warning(f"Duplicated property {prop_name}") 68 continue 69 70 def mk_setter_getter(prop_or_callback_name: str) -> property: 71 def getter(self: Component) -> Any: 72 return self.__instance__.get_global_property( 73 global_name, prop_or_callback_name 74 ) 75 76 def setter(self: Component, value: Any) -> None: 77 self.__instance__.set_global_property( 78 global_name, prop_or_callback_name, value 79 ) 80 81 return property(getter, setter) 82 83 properties_and_callbacks[python_prop] = mk_setter_getter(prop_name) 84 85 for callback_name in compdef.global_callbacks(global_name): 86 python_prop = _normalize_prop(callback_name) 87 if python_prop in properties_and_callbacks: 88 logging.warning(f"Duplicated property {prop_name}") 89 continue 90 91 def mk_setter_getter(prop_or_callback_name: str) -> property: 92 def getter(self: Component) -> typing.Callable[..., Any]: 93 def call(*args: Any) -> Any: 94 return self.__instance__.invoke_global( 95 global_name, prop_or_callback_name, *args 96 ) 97 98 return call 99 100 def setter(self: Component, value: typing.Callable[..., Any]) -> None: 101 self.__instance__.set_global_callback( 102 global_name, prop_or_callback_name, value 103 ) 104 105 return property(getter, setter) 106 107 properties_and_callbacks[python_prop] = mk_setter_getter(callback_name) 108 109 for function_name in compdef.global_functions(global_name): 110 python_prop = _normalize_prop(function_name) 111 if python_prop in properties_and_callbacks: 112 logging.warning(f"Duplicated function {prop_name}") 113 continue 114 115 def mk_getter(function_name: str) -> property: 116 def getter(self: Component) -> typing.Callable[..., Any]: 117 def call(*args: Any) -> Any: 118 return self.__instance__.invoke_global( 119 global_name, function_name, *args 120 ) 121 122 return call 123 124 return property(getter) 125 126 properties_and_callbacks[python_prop] = mk_getter(function_name) 127 128 return type("SlintGlobalClassWrapper", (), properties_and_callbacks) 129 130 131def _build_class( 132 compdef: native.ComponentDefinition, 133) -> typing.Callable[..., Component]: 134 def cls_init(self: Component, **kwargs: Any) -> Any: 135 self.__instance__ = compdef.create() 136 for name, value in self.__class__.__dict__.items(): 137 if hasattr(value, "slint.callback"): 138 callback_info = getattr(value, "slint.callback") 139 name = callback_info["name"] 140 141 def mk_callback( 142 self: Any, callback: typing.Callable[..., Any] 143 ) -> typing.Callable[..., Any]: 144 def invoke(*args: Any, **kwargs: Any) -> Any: 145 return callback(self, *args, **kwargs) 146 147 return invoke 148 149 if "global_name" in callback_info: 150 self.__instance__.set_global_callback( 151 callback_info["global_name"], name, mk_callback(self, value) 152 ) 153 else: 154 self.__instance__.set_callback(name, mk_callback(self, value)) 155 156 for prop, val in kwargs.items(): 157 setattr(self, prop, val) 158 159 properties_and_callbacks: dict[Any, Any] = {"__init__": cls_init} 160 161 for prop_name in compdef.properties.keys(): 162 python_prop = _normalize_prop(prop_name) 163 if python_prop in properties_and_callbacks: 164 logging.warning(f"Duplicated property {prop_name}") 165 continue 166 167 def mk_setter_getter(prop_or_callback_name: str) -> property: 168 def getter(self: Component) -> Any: 169 return self.__instance__.get_property(prop_or_callback_name) 170 171 def setter(self: Component, value: Any) -> None: 172 self.__instance__.set_property(prop_or_callback_name, value) 173 174 return property(getter, setter) 175 176 properties_and_callbacks[python_prop] = mk_setter_getter(prop_name) 177 178 for callback_name in compdef.callbacks: 179 python_prop = _normalize_prop(callback_name) 180 if python_prop in properties_and_callbacks: 181 logging.warning(f"Duplicated property {prop_name}") 182 continue 183 184 def mk_setter_getter(prop_or_callback_name: str) -> property: 185 def getter(self: Component) -> typing.Callable[..., Any]: 186 def call(*args: Any) -> Any: 187 return self.__instance__.invoke(prop_or_callback_name, *args) 188 189 return call 190 191 def setter(self: Component, value: typing.Callable[..., Any]) -> None: 192 self.__instance__.set_callback(prop_or_callback_name, value) 193 194 return property(getter, setter) 195 196 properties_and_callbacks[python_prop] = mk_setter_getter(callback_name) 197 198 for function_name in compdef.functions: 199 python_prop = _normalize_prop(function_name) 200 if python_prop in properties_and_callbacks: 201 logging.warning(f"Duplicated function {prop_name}") 202 continue 203 204 def mk_getter(function_name: str) -> property: 205 def getter(self: Component) -> typing.Callable[..., Any]: 206 def call(*args: Any) -> Any: 207 return self.__instance__.invoke(function_name, *args) 208 209 return call 210 211 return property(getter) 212 213 properties_and_callbacks[python_prop] = mk_getter(function_name) 214 215 for global_name in compdef.globals: 216 global_class = _build_global_class(compdef, global_name) 217 218 def mk_global(global_class: typing.Callable[..., Any]) -> property: 219 def global_getter(self: Component) -> Any: 220 wrapper = global_class() 221 setattr(wrapper, "__instance__", self.__instance__) 222 return wrapper 223 224 return property(global_getter) 225 226 properties_and_callbacks[global_name] = mk_global(global_class) 227 228 return type("SlintClassWrapper", (Component,), properties_and_callbacks) 229 230 231def _build_struct(name: str, struct_prototype: native.PyStruct) -> type: 232 def new_struct(cls: Any, *args: Any, **kwargs: Any) -> native.PyStruct: 233 inst = copy.copy(struct_prototype) 234 235 for prop, val in kwargs.items(): 236 setattr(inst, prop, val) 237 238 return inst 239 240 type_dict = { 241 "__new__": new_struct, 242 } 243 244 return type(name, (), type_dict) 245 246 247def load_file( 248 path: str | os.PathLike[Any] | pathlib.Path, 249 quiet: bool = False, 250 style: typing.Optional[str] = None, 251 include_paths: typing.Optional[ 252 typing.List[str | os.PathLike[Any] | pathlib.Path] 253 ] = None, 254 library_paths: typing.Optional[ 255 typing.Dict[str, str | os.PathLike[Any] | pathlib.Path] 256 ] = None, 257 translation_domain: typing.Optional[str] = None, 258) -> types.SimpleNamespace: 259 """This function is the low-level entry point into Slint for Python. Loads the `.slint` file at the specified `path` 260 and returns a namespace with all exported components as Python classes, as well as enums and structs. 261 262 * `quiet`: Set to true to prevent any warnings during compilation to be printed to stderr. 263 * `style`: Set this to use a specific a widget style. 264 * `include_paths`: Additional include paths that will be used to look up `.slint` files imported from other `.slint` files. 265 * `library_paths`: A dictionary that maps library names to their location in the file system. This is used to look up library imports, 266 such as `import { MyButton } from "@mylibrary";`. 267 * `translation_domain`: The domain to use for looking up the catalogue run-time translations. This must match the translation domain 268 used when extracting translations with `slint-tr-extractor`. 269 270 """ 271 272 compiler = native.Compiler() 273 274 if style is not None: 275 compiler.style = style 276 if include_paths is not None: 277 compiler.include_paths = include_paths 278 if library_paths is not None: 279 compiler.library_paths = library_paths 280 if translation_domain is not None: 281 compiler.translation_domain = translation_domain 282 283 result = compiler.build_from_path(path) 284 285 diagnostics = result.diagnostics 286 if diagnostics: 287 if not quiet: 288 for diag in diagnostics: 289 if diag.level == native.DiagnosticLevel.Warning: 290 logging.warning(diag) 291 292 errors = [ 293 diag 294 for diag in diagnostics 295 if diag.level == native.DiagnosticLevel.Error 296 ] 297 if errors: 298 raise CompileError(f"Could not compile {path}", diagnostics) 299 300 module = types.SimpleNamespace() 301 for comp_name in result.component_names: 302 wrapper_class = _build_class(result.component(comp_name)) 303 304 setattr(module, comp_name, wrapper_class) 305 306 for name, struct_or_enum_prototype in result.structs_and_enums.items(): 307 name = _normalize_prop(name) 308 struct_wrapper = _build_struct(name, struct_or_enum_prototype) 309 setattr(module, name, struct_wrapper) 310 311 for orig_name, new_name in result.named_exports: 312 orig_name = _normalize_prop(orig_name) 313 new_name = _normalize_prop(new_name) 314 setattr(module, new_name, getattr(module, orig_name)) 315 316 return module 317 318 319class SlintAutoLoader: 320 def __init__(self, base_dir: str | None = None): 321 self.local_dirs: typing.List[str] | None = None 322 if base_dir: 323 self.local_dirs = [base_dir] 324 325 def __getattr__(self, name: str) -> Any: 326 for path in self.local_dirs or sys.path: 327 dir_candidate = os.path.join(path, name) 328 if os.path.isdir(dir_candidate): 329 loader = SlintAutoLoader(dir_candidate) 330 setattr(self, name, loader) 331 return loader 332 333 file_candidate = dir_candidate + ".slint" 334 if os.path.isfile(file_candidate): 335 type_namespace = load_file(file_candidate) 336 setattr(self, name, type_namespace) 337 return type_namespace 338 339 dir_candidate = os.path.join(path, name.replace("_", "-")) 340 file_candidate = dir_candidate + ".slint" 341 if os.path.isfile(file_candidate): 342 type_namespace = load_file(file_candidate) 343 setattr(self, name, type_namespace) 344 return type_namespace 345 346 return None 347 348 349loader = SlintAutoLoader() 350"""The `loader` object is a global object that can be used to load Slint files from the file system. It exposes two stages of attributes: 3511. Any lookup of an attribute in the loader will try to match a file in `sys.path` with the `.slint` extension. For example `loader.my_component` will look for a file `my_component.slint` in the directories in `sys.path`. 3522. Any lookup in the object returned by the first stage will try to match an exported component in the loaded file, or a struct or enum. For example `loader.my_component.MyComponent` will look for an *exported* component named `MyComponent` in the file `my_component.slint`. 353 354Note that the first entry in the module search path `sys.path` is the directory that contains the input script. 355 356Example: 357```python 358import slint 359# Look for a file `main.slint` in the current directory, 360# #load & compile it, and instantiate the exported `MainWindow` component 361main_window = slint.loader.main_window.MainWindow() 362main_window.show() 363... 364``` 365""" 366 367 368def _callback_decorator( 369 callable: typing.Callable[..., Any], info: typing.Dict[str, Any] 370) -> typing.Callable[..., Any]: 371 if "name" not in info: 372 info["name"] = callable.__name__ 373 setattr(callable, "slint.callback", info) 374 return callable 375 376 377def callback( 378 global_name: str | None = None, name: str | None = None 379) -> typing.Callable[..., Any]: 380 """Use the callback decorator to mark a method as a callback that can be invoked from the Slint component. 381 382 For the decorator to work, the method must be a member of a class that is Slint component. 383 384 Example: 385 ```python 386 import slint 387 388 class AppMainWindow(slint.loader.main_window.MainWindow): 389 390 # Automatically connected to a callback button_clicked() 391 # in main_window.slint's MainWindow. 392 @slint.callback() 393 def button_clicked(self): 394 print("Button clicked") 395 396 ... 397 ``` 398 399 Use the `name` parameter to specify the name of the callback in the Slint component, if the name of the 400 Python method differs from the name of the callback in the Slint component. 401 402 Use the `global_name` parameter to specify the name of the global in the Slint component, if the callback 403 is to be set on a Slint global object. 404 """ 405 406 if callable(global_name): 407 callback = global_name 408 return _callback_decorator(callback, {}) 409 else: 410 info = {} 411 if name: 412 info["name"] = name 413 if global_name: 414 info["global_name"] = global_name 415 return lambda callback: _callback_decorator(callback, info) 416 417 418def set_xdg_app_id(app_id: str) -> None: 419 """Sets the application id for use on Wayland or X11 with [xdg](https://specifications.freedesktop.org/desktop-entry-spec/latest/) 420 compliant window managers. This must be set before the window is shown, and has only an effect on Wayland or X11.""" 421 422 native.set_xdg_app_id(app_id) 423 424 425__all__ = [ 426 "CompileError", 427 "Component", 428 "load_file", 429 "loader", 430 "Image", 431 "Color", 432 "Brush", 433 "Model", 434 "ListModel", 435 "Timer", 436 "TimerMode", 437 "set_xdg_app_id", 438 "callback", 439]
24class CompileError(Exception): 25 message: str 26 """The error message that produced this compile error.""" 27 28 diagnostics: list[native.PyDiagnostic] 29 """A list of detailed diagnostics that were produced as part of the compilation.""" 30 31 def __init__(self, message: str, diagnostics: list[native.PyDiagnostic]): 32 """@private""" 33 self.message = message 34 self.diagnostics = diagnostics
Common base class for all non-exit exceptions.
37class Component: 38 """Component is the base class for all instances of Slint components. Use the member functions to show or hide the 39 window, or spin the event loop.""" 40 41 __instance__: native.ComponentInstance 42 43 def show(self) -> None: 44 """Shows the window on the screen.""" 45 46 self.__instance__.show() 47 48 def hide(self) -> None: 49 """Hides the window from the screen.""" 50 51 self.__instance__.hide() 52 53 def run(self) -> None: 54 """Shows the window, runs the event loop, hides it when the loop is quit, and returns.""" 55 self.__instance__.run()
Component is the base class for all instances of Slint components. Use the member functions to show or hide the window, or spin the event loop.
248def load_file( 249 path: str | os.PathLike[Any] | pathlib.Path, 250 quiet: bool = False, 251 style: typing.Optional[str] = None, 252 include_paths: typing.Optional[ 253 typing.List[str | os.PathLike[Any] | pathlib.Path] 254 ] = None, 255 library_paths: typing.Optional[ 256 typing.Dict[str, str | os.PathLike[Any] | pathlib.Path] 257 ] = None, 258 translation_domain: typing.Optional[str] = None, 259) -> types.SimpleNamespace: 260 """This function is the low-level entry point into Slint for Python. Loads the `.slint` file at the specified `path` 261 and returns a namespace with all exported components as Python classes, as well as enums and structs. 262 263 * `quiet`: Set to true to prevent any warnings during compilation to be printed to stderr. 264 * `style`: Set this to use a specific a widget style. 265 * `include_paths`: Additional include paths that will be used to look up `.slint` files imported from other `.slint` files. 266 * `library_paths`: A dictionary that maps library names to their location in the file system. This is used to look up library imports, 267 such as `import { MyButton } from "@mylibrary";`. 268 * `translation_domain`: The domain to use for looking up the catalogue run-time translations. This must match the translation domain 269 used when extracting translations with `slint-tr-extractor`. 270 271 """ 272 273 compiler = native.Compiler() 274 275 if style is not None: 276 compiler.style = style 277 if include_paths is not None: 278 compiler.include_paths = include_paths 279 if library_paths is not None: 280 compiler.library_paths = library_paths 281 if translation_domain is not None: 282 compiler.translation_domain = translation_domain 283 284 result = compiler.build_from_path(path) 285 286 diagnostics = result.diagnostics 287 if diagnostics: 288 if not quiet: 289 for diag in diagnostics: 290 if diag.level == native.DiagnosticLevel.Warning: 291 logging.warning(diag) 292 293 errors = [ 294 diag 295 for diag in diagnostics 296 if diag.level == native.DiagnosticLevel.Error 297 ] 298 if errors: 299 raise CompileError(f"Could not compile {path}", diagnostics) 300 301 module = types.SimpleNamespace() 302 for comp_name in result.component_names: 303 wrapper_class = _build_class(result.component(comp_name)) 304 305 setattr(module, comp_name, wrapper_class) 306 307 for name, struct_or_enum_prototype in result.structs_and_enums.items(): 308 name = _normalize_prop(name) 309 struct_wrapper = _build_struct(name, struct_or_enum_prototype) 310 setattr(module, name, struct_wrapper) 311 312 for orig_name, new_name in result.named_exports: 313 orig_name = _normalize_prop(orig_name) 314 new_name = _normalize_prop(new_name) 315 setattr(module, new_name, getattr(module, orig_name)) 316 317 return module
This function is the low-level entry point into Slint for Python. Loads the .slint
file at the specified path
and returns a namespace with all exported components as Python classes, as well as enums and structs.
quiet
: Set to true to prevent any warnings during compilation to be printed to stderr.style
: Set this to use a specific a widget style.include_paths
: Additional include paths that will be used to look up.slint
files imported from other.slint
files.library_paths
: A dictionary that maps library names to their location in the file system. This is used to look up library imports, such asimport { MyButton } from "@mylibrary";
.translation_domain
: The domain to use for looking up the catalogue run-time translations. This must match the translation domain used when extracting translations withslint-tr-extractor
.
The loader
object is a global object that can be used to load Slint files from the file system. It exposes two stages of attributes:
- Any lookup of an attribute in the loader will try to match a file in
sys.path
with the.slint
extension. For exampleloader.my_component
will look for a filemy_component.slint
in the directories insys.path
. - Any lookup in the object returned by the first stage will try to match an exported component in the loaded file, or a struct or enum. For example
loader.my_component.MyComponent
will look for an exported component namedMyComponent
in the filemy_component.slint
.
Note that the first entry in the module search path sys.path
is the directory that contains the input script.
Example:
import slint
# Look for a file `main.slint` in the current directory,
# #load & compile it, and instantiate the exported `MainWindow` component
main_window = slint.loader.main_window.MainWindow()
main_window.show()
...
Image objects can be set on Slint Image elements for display. Construct Image objects from a path to an
image file on disk, using Image.load_from_path
.
Loads the image from the specified path. Returns None if the image can't be loaded.
A Color object represents a color in the RGB color space with an alpha. Each color channel as well as the alpha is represented as an 8-bit integer. The alpha channel is 0 for fully transparent and 255 for fully opaque.
Construct colors from either a CSS color string, or by specifying the red, green, blue, and (optional) alpha channels in a dict.
Returns a new color that is brighter than this color by the given factor.
Returns a new color that is darker than this color by the given factor.
Returns a new version of this color with the opacity decreased by factor
.
The transparency is obtained by multiplying the alpha channel by (1 - factor)
.
A brush is a data structure that is used to describe how a shape, such as a rectangle, path or even text, shall be filled. A brush can also be applied to the outline of a shape, that means the fill of the outline itself.
Brushes can only be constructed from solid colors. This is a restriction we anticipate to lift in the future, to programmatically also declare gradients.
Returns true if this brush contains a fully transparent color (alpha value is zero).
Returns a new version of this brush that has the brightness increased
by the specified factor. This is done by calling Color.brighter
on
all the colors of this brush.
Returns a new version of this brush that has the brightness decreased
by the specified factor. This is done by calling Color.darker
on
all the color of this brush.
Returns a new version of this brush with the opacity decreased by factor
.
The transparency is obtained by multiplying the alpha channel by (1 - factor)
.
See also Color.transparentize
.
12class Model[T](native.PyModelBase, Iterable[T]): 13 """Model is the base class for feeding dynamic data into Slint views. 14 15 Subclass Model to implement your own models, or use `ListModel` to wrap a list. 16 17 Models are iterable and can be used in for loops.""" 18 19 def __new__(cls, *args: Any) -> "Model[T]": 20 return super().__new__(cls) 21 22 def __init__(self) -> None: 23 self.init_self(self) 24 25 def __len__(self) -> int: 26 return self.row_count() 27 28 def __getitem__(self, index: int) -> typing.Optional[T]: 29 return self.row_data(index) 30 31 def __setitem__(self, index: int, value: T) -> None: 32 self.set_row_data(index, value) 33 34 def __iter__(self) -> Iterator[T]: 35 return ModelIterator(self) 36 37 def set_row_data(self, row: int, value: T) -> None: 38 """Call this method on mutable models to change the data for the given row. 39 The UI will also call this method when modifying a model's data. 40 Re-implement this method in a sub-class to handle the change.""" 41 super().set_row_data(row, value) 42 43 @abstractmethod 44 def row_data(self, row: int) -> typing.Optional[T]: 45 """Returns the data for the given row. 46 Re-implement this method in a sub-class to provide the data.""" 47 return cast(T, super().row_data(row)) 48 49 def notify_row_changed(self, row: int) -> None: 50 """Call this method from a sub-class to notify the views that a row has changed.""" 51 super().notify_row_changed(row) 52 53 def notify_row_removed(self, row: int, count: int) -> None: 54 """Call this method from a sub-class to notify the views that 55 `count` rows have been removed starting at `row`.""" 56 super().notify_row_removed(row, count) 57 58 def notify_row_added(self, row: int, count: int) -> None: 59 """Call this method from a sub-class to notify the views that 60 `count` rows have been added starting at `row`.""" 61 super().notify_row_added(row, count)
Model is the base class for feeding dynamic data into Slint views.
Subclass Model to implement your own models, or use ListModel
to wrap a list.
Models are iterable and can be used in for loops.
37 def set_row_data(self, row: int, value: T) -> None: 38 """Call this method on mutable models to change the data for the given row. 39 The UI will also call this method when modifying a model's data. 40 Re-implement this method in a sub-class to handle the change.""" 41 super().set_row_data(row, value)
Call this method on mutable models to change the data for the given row. The UI will also call this method when modifying a model's data. Re-implement this method in a sub-class to handle the change.
43 @abstractmethod 44 def row_data(self, row: int) -> typing.Optional[T]: 45 """Returns the data for the given row. 46 Re-implement this method in a sub-class to provide the data.""" 47 return cast(T, super().row_data(row))
Returns the data for the given row. Re-implement this method in a sub-class to provide the data.
Call this method from a sub-class to notify the views that a row has changed.
64class ListModel[T](Model[T]): 65 """ListModel is a `Model` that stores its data in a Python list. 66 67 Construct a ListMode from an iterable (such as a list itself). 68 Use `ListModel.append()` to add items to the model, and use the 69 `del` statement to remove items. 70 71 Any changes to the model are automatically reflected in the views 72 in UI they're used with. 73 """ 74 75 def __init__(self, iterable: typing.Optional[Iterable[T]] = None): 76 """Constructs a new ListModel from the give iterable. All the values 77 the iterable produces are stored in a list.""" 78 79 super().__init__() 80 if iterable is not None: 81 self.list = list(iterable) 82 else: 83 self.list = [] 84 85 def row_count(self) -> int: 86 return len(self.list) 87 88 def row_data(self, row: int) -> typing.Optional[T]: 89 return self.list[row] 90 91 def set_row_data(self, row: int, data: T) -> None: 92 self.list[row] = data 93 super().notify_row_changed(row) 94 95 def __delitem__(self, key: int | slice) -> None: 96 if isinstance(key, slice): 97 start, stop, step = key.indices(len(self.list)) 98 del self.list[key] 99 count = len(range(start, stop, step)) 100 super().notify_row_removed(start, count) 101 else: 102 del self.list[key] 103 super().notify_row_removed(key, 1) 104 105 def append(self, value: T) -> None: 106 """Appends the value to the end of the list.""" 107 index = len(self.list) 108 self.list.append(value) 109 super().notify_row_added(index, 1)
ListModel is a Model
that stores its data in a Python list.
Construct a ListMode from an iterable (such as a list itself).
Use ListModel.append()
to add items to the model, and use the
del
statement to remove items.
Any changes to the model are automatically reflected in the views in UI they're used with.
75 def __init__(self, iterable: typing.Optional[Iterable[T]] = None): 76 """Constructs a new ListModel from the give iterable. All the values 77 the iterable produces are stored in a list.""" 78 79 super().__init__() 80 if iterable is not None: 81 self.list = list(iterable) 82 else: 83 self.list = []
Constructs a new ListModel from the give iterable. All the values the iterable produces are stored in a list.
Returns the data for the given row. Re-implement this method in a sub-class to provide the data.
91 def set_row_data(self, row: int, data: T) -> None: 92 self.list[row] = data 93 super().notify_row_changed(row)
Call this method on mutable models to change the data for the given row. The UI will also call this method when modifying a model's data. Re-implement this method in a sub-class to handle the change.
Timer is a handle to the timer system that triggers a callback after a specified period of time.
Use Timer.start()
to create a timer that that repeatedly triggers a callback, or
[Timer::single_shot
] to trigger a callback only once.
The timer will automatically stop when garbage collected. You must keep the Timer object around for as long as you want the timer to keep firing.
Timers can only be used in the thread that runs the Slint event loop. They don't fire if used in another thread.
Starts the timer with the given mode and interval, in order for the callback to called when the timer fires. If the timer has been started previously and not fired yet, then it will be restarted.
Arguments:
mode
: The timer mode to apply, i.e. whether to repeatedly fire the timer or just once.interval
: The duration from now until when the timer should firethe first time, and subsequently forTimerMode.Repeated
timers.callback
: The function to call when the time has been reached or exceeded.
Starts the timer with the duration and the callback to called when the timer fires. It is fired only once and then deleted.
Arguments:
duration
: The duration from now until when the timer should fire.callback
: The function to call when the time has been reached or exceeded.
Stops the previously started timer. Does nothing if the timer has never been started.
Restarts the timer. If the timer was previously started by calling Timer.start()
with a duration and callback, then the time when the callback will be next invoked
is re-calculated to be in the specified duration relative to when this function is called.
Does nothing if the timer was never started.
The duration of timer.
When setting this property and the timer is running (see Timer.running
),
then the time when the callback will be next invoked is re-calculated to be in the
specified duration relative to when this property is set.
The TimerMode specifies what should happen after the timer fired.
Used by the Timer.start()
function.
419def set_xdg_app_id(app_id: str) -> None: 420 """Sets the application id for use on Wayland or X11 with [xdg](https://specifications.freedesktop.org/desktop-entry-spec/latest/) 421 compliant window managers. This must be set before the window is shown, and has only an effect on Wayland or X11.""" 422 423 native.set_xdg_app_id(app_id)
Sets the application id for use on Wayland or X11 with xdg compliant window managers. This must be set before the window is shown, and has only an effect on Wayland or X11.
378def callback( 379 global_name: str | None = None, name: str | None = None 380) -> typing.Callable[..., Any]: 381 """Use the callback decorator to mark a method as a callback that can be invoked from the Slint component. 382 383 For the decorator to work, the method must be a member of a class that is Slint component. 384 385 Example: 386 ```python 387 import slint 388 389 class AppMainWindow(slint.loader.main_window.MainWindow): 390 391 # Automatically connected to a callback button_clicked() 392 # in main_window.slint's MainWindow. 393 @slint.callback() 394 def button_clicked(self): 395 print("Button clicked") 396 397 ... 398 ``` 399 400 Use the `name` parameter to specify the name of the callback in the Slint component, if the name of the 401 Python method differs from the name of the callback in the Slint component. 402 403 Use the `global_name` parameter to specify the name of the global in the Slint component, if the callback 404 is to be set on a Slint global object. 405 """ 406 407 if callable(global_name): 408 callback = global_name 409 return _callback_decorator(callback, {}) 410 else: 411 info = {} 412 if name: 413 info["name"] = name 414 if global_name: 415 info["global_name"] = global_name 416 return lambda callback: _callback_decorator(callback, info)
Use the callback decorator to mark a method as a callback that can be invoked from the Slint component.
For the decorator to work, the method must be a member of a class that is Slint component.
Example:
import slint
class AppMainWindow(slint.loader.main_window.MainWindow):
# Automatically connected to a callback button_clicked()
# in main_window.slint's MainWindow.
@slint.callback()
def button_clicked(self):
print("Button clicked")
...
Use the name
parameter to specify the name of the callback in the Slint component, if the name of the
Python method differs from the name of the callback in the Slint component.
Use the global_name
parameter to specify the name of the global in the Slint component, if the callback
is to be set on a Slint global object.