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
appwindow.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 `appwindow.slint`.
class App(slint.loader.appwindow.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: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.
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)
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
import MyComponent from my_component_slint
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
import my_component_slint
class Component(my_component_slint.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 |
Structures are mapped to Python dictionaries where each structure field is an item. |
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)
.
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
Hashes for slint-1.6.0a8-cp310-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 434e4031b1a2bbc7d758335171f8f9230cd140205fdfca62ea0a96fc81ce9de6 |
|
MD5 | bda8a691c71a0c1f6807504e7635dc15 |
|
BLAKE2b-256 | bf59d51f5c31a5d94bb423bc14ae55a6490579b48a6e25e08671eca86a3174ef |
Hashes for slint-1.6.0a8-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff84c5c48fbf1860e7d6b3a2687b0b871626b27197d1cd36087aac69fbe3fdfa |
|
MD5 | 081a40fe55eddebc7b1ab043e927184c |
|
BLAKE2b-256 | b5c78f2be370252e520c7408eb85ae0a75b435eea632126effc6a6ced42d063e |
Hashes for slint-1.6.0a8-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c927cbe291df4c8a965ff0da2032a54c5fa1074eef4653f036963ed66283b12 |
|
MD5 | 09f953f532b66778e3466dc375adc517 |
|
BLAKE2b-256 | 16de95138bd495e94596c36d96e46591b5990957aba49fa78ec75de08661046f |
Hashes for slint-1.6.0a8-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8bd1df7380da81011359cecb016d433b66ad77c8bf8bfdc146737c3f285ee3f |
|
MD5 | 8c74629c02404934347a748868872959 |
|
BLAKE2b-256 | 0094722e92a8279a78dfcbbcf8f4d6ea856d30d330ec4ff6b426d0f3a18746c4 |
Hashes for slint-1.6.0a8-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cb543f8475f60ac73aaa88cfdd7de842ecac247ff030cf748bd90c43fdcc1b1 |
|
MD5 | 58d3ac67d7b1dfdc8e2732aff6642632 |
|
BLAKE2b-256 | 0b75864c989f479fb4f2bb5b5cdd12ba78a45d53ff95b6d2cbae4b8451058ac8 |
Hashes for slint-1.6.0a8-cp310-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79e2685a183d8a800e15e35334fa1b6965f860985532825319c406db8d6f637a |
|
MD5 | ef220ea235b39d81acec9c0e329090e8 |
|
BLAKE2b-256 | 4629b52f3a927800aaeabd115fa92b262d4fba996201ddef8728c1e1f3f8c15b |