Slint Python integration
Project description
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
from the Python Package Index:
pip install slint
The installation will use binaries provided vi macOS, Windows, and Linux for various architectures. If your target platform is not covered by binaries,
pip
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 examples/printerdemo/python
pipenv update
pipenv run python main.py
Quick Start
- Add Slint Python Package Index to your Python project:
pipenv install 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
pipenv run python 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:
export global PrinterJobQueue {
in-out property <int> job-count;
}
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file slint-1.8.0a1.tar.gz
.
File metadata
- Download URL: slint-1.8.0a1.tar.gz
- Upload date:
- Size: 1.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2676191242c36393314e03944197021c38f89963207834109efbd50b563e20c |
|
MD5 | 67d69ec3f779600ecd45f3ef3565f0d6 |
|
BLAKE2b-256 | a9fa7e09870055a54cf4c3c6607343cc616cb98ef705392742abd6096fd243fa |
File details
Details for the file slint-1.8.0a1-cp310-abi3-win_amd64.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 11.7 MB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed71f4456b7d11c0c6a3a3a78f3a33ae579478a53397138c3120ff647350b994 |
|
MD5 | aae9aface15dd22995ffd5c7488d4c7f |
|
BLAKE2b-256 | 06a6152e9dcba679f07973f520a93da70235de5d3f083d3b26b3d71a411a95ad |
File details
Details for the file slint-1.8.0a1-cp310-abi3-manylinux_2_35_x86_64.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-manylinux_2_35_x86_64.whl
- Upload date:
- Size: 16.0 MB
- Tags: CPython 3.10+, manylinux: glibc 2.35+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08f42b77dea694d2373c507ee4e90e865bf03fdc86cb6274710bcc1bc2082d73 |
|
MD5 | bb70cba83a8666eb2bf2f60c53a6647f |
|
BLAKE2b-256 | eb563a2d6a7de276cd1fe9b0005d2d46b5f33d0902f754503b72d7cb71fb9e2e |
File details
Details for the file slint-1.8.0a1-cp310-abi3-manylinux_2_31_armv7l.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-manylinux_2_31_armv7l.whl
- Upload date:
- Size: 15.1 MB
- Tags: CPython 3.10+, manylinux: glibc 2.31+ ARMv7l
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59a31f14c423dbeede53c57784a05947841e0b99256732396ff26820cd52050a |
|
MD5 | 708b85cdfa36fcd95a3c83a7166e4e7e |
|
BLAKE2b-256 | 51bd7361b828f9268acf524a5f1b2a4d8cd84637a7c975e2a6b616d6fe08386f |
File details
Details for the file slint-1.8.0a1-cp310-abi3-manylinux_2_31_aarch64.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-manylinux_2_31_aarch64.whl
- Upload date:
- Size: 15.5 MB
- Tags: CPython 3.10+, manylinux: glibc 2.31+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9d907b86e48ba08df94cd8d4c0f158d51275534389d48de8d8ea9cb64723177 |
|
MD5 | b31be70f984af00c910b2a5703b5cc68 |
|
BLAKE2b-256 | 599518b99b33547a5d66ad3d1e2ee3a2f0d987883fa60b05bb6ae2e285ba3822 |
File details
Details for the file slint-1.8.0a1-cp310-abi3-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-macosx_11_0_x86_64.whl
- Upload date:
- Size: 11.6 MB
- Tags: CPython 3.10+, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8195ba55b5bd5210d0c0a602a6bdc9a7b891f02c62c3736a80ab3136c3905fd4 |
|
MD5 | d210f92a196352919a819470a01e7983 |
|
BLAKE2b-256 | 6fb2cba0d8025e7dbc315b8494006c69e9d9a980dfe5ce0c46e597603926c1b1 |
File details
Details for the file slint-1.8.0a1-cp310-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: slint-1.8.0a1-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 11.1 MB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71389924cb95bcd603156fa5a9dd4bf362cfb88591f568f03fe2b1cd843371d7 |
|
MD5 | b159c2f831dc97dab40d39a00f8f8e0d |
|
BLAKE2b-256 | acd6baa5662477a32663168aa505b1e52b10eba5c72049cfec9564248500eac6 |