Skip to main content

Slint Python integration

Project description

Slint-python (Beta)

Slint is a UI toolkit that supports different programming languages. Slint-python is the integration with Python.

Warning Slint-python is in a beta phase of development: The APIs while mostly stable, may be subject to further changes. Any changes will be documented in the ChangeLog.

You can track the 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 uv or pip from the Python Package Index:

uv add slint

The installation uses binaries provided for 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 will then need some software development tools on your machine, as well as Rust.

Quick Start

  1. Create a new project with uv init.
  2. Add the Slint Python package to your Python project: uv add slint
  3. 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();
            }
        }
    }
}
  1. 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()
  1. 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:

  1. Call slint.load_file("app.slint"). The returned object is a namespace, that provides the MainWindow class as well as any other explicitly exported component that inherits Window:

    import slint
    components = slint.load_file("app.slint")
    main_window = components.MainWindow()
    
  2. Use Slint's auto-loader, which lazily loads .slint files from sys.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 in sys.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 with load_file and the namespace is returned.

    If the file name contains a dash, like app-window.slint, an attribute lookup for app_window tries to locate app_window.slint and then fall back to app-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:

export global PrinterJobQueue {
    in-out property <int> job-count;
}

Access it as a property on the component instance by its name:

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 has their own associated globals singletons.

Setting and Invoking Callbacks

Callbacks declared in .slint files are visible as callable properties on the component instance. Invoke them as functions to invoke the callback, and assign Python callables to set the callback handler.

In Slint, callbacks are defined using the callback keyword and can be connected to another component's callback 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 functions.

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 argument, if 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

Each type used for properties in the Slint Language translates to a specific type in Python. The following table summarizes the 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

You can set array properties 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 or inserting rows, call notify_row_added(row, count) on the super class. Similarly, when removing rows, notify Slint by calling notify_row_removed(row, count).

Structs

Structs declared in Slint and exposed to Python via export are then accessible in the namespace that is 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 as follows:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slint-1.12.1b1.tar.gz (1.9 MB view details)

Uploaded Source

Built Distributions

slint-1.12.1b1-cp310-abi3-win_amd64.whl (12.2 MB view details)

Uploaded CPython 3.10+Windows x86-64

slint-1.12.1b1-cp310-abi3-manylinux_2_35_x86_64.whl (16.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.35+ x86-64

slint-1.12.1b1-cp310-abi3-manylinux_2_31_armv7l.whl (15.9 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARMv7l

slint-1.12.1b1-cp310-abi3-manylinux_2_31_aarch64.whl (16.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARM64

slint-1.12.1b1-cp310-abi3-macosx_11_0_x86_64.whl (12.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ x86-64

slint-1.12.1b1-cp310-abi3-macosx_11_0_arm64.whl (11.7 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphonesimulator.whl (9.0 MB view details)

Uploaded CPython 3.10+iOS 13.0+ ARM64 Simulator

slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphoneos.whl (8.8 MB view details)

Uploaded CPython 3.10+iOS 13.0+ ARM64 Device

File details

Details for the file slint-1.12.1b1.tar.gz.

File metadata

  • Download URL: slint-1.12.1b1.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for slint-1.12.1b1.tar.gz
Algorithm Hash digest
SHA256 93150ae98986021389682101bb2d3d18a4259733f0c108d5c9c7558b3250d8c7
MD5 625cc6ff5359d50a2637476ab894ee7c
BLAKE2b-256 8ef6e04a2d0b1d40e04a195ed915171517709c01f73f19e1c6045116c9ee589b

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1.tar.gz:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: slint-1.12.1b1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.2 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 17a927bcd4044f54dd43d978b187cfb6b2bbb407af8cdac7300e4a48e1b7f6ab
MD5 e68a88f3760ce7518a2b1ae46cd2f157
BLAKE2b-256 719d2ebe69d0fb723206f9b22d2be382130a24ccac9d370780d060800e6f0576

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-win_amd64.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 fe7fb24c5991e6ffcf2311bcedf5491d7847ff96b316a60ec35d033553ecf8f0
MD5 527b564d41779390f0f04bcb9f40be38
BLAKE2b-256 af48e361a02482afbf001cd7082c8a56a11f4b90e92598fc75dcc2c0a58566d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-manylinux_2_35_x86_64.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-manylinux_2_31_armv7l.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-manylinux_2_31_armv7l.whl
Algorithm Hash digest
SHA256 0c64a2ab95e17c12ae775ec21d6314c47bd0cb415ca38cf02da6d06122cde5e2
MD5 d7472e739ac9d79a2f3ab94372fc46f3
BLAKE2b-256 fed77378a06b90edc25341bc4b012fae43ea87521af294d65a84ab9319de030f

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-manylinux_2_31_armv7l.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 5153954c7d53e28f3aa56eaca8b306d1481dfac4a099c6b3f6f81a52eda66da2
MD5 b237b9084b42331418a8df8fb8ad4bec
BLAKE2b-256 58ecfed21cdf33fa251553d41bc26d84d71213e7d6d9ba6e7965ef994ba108e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-manylinux_2_31_aarch64.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 8a19f19250f67872b0c82d0c658edb091852df847d1547779b480552d7ea9e95
MD5 0549a861f61ef29b1276338a16c87af6
BLAKE2b-256 45602c03772e903adbb8bc129f1aac3a7702b97962984ecb69b3b6c8a17cbe82

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-macosx_11_0_x86_64.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 68eae7d7e69b570fb6667e6d6e4186b37e380110c1458e5c8955e85ab14cf81b
MD5 e308f73e692756f3468652d0cc38f91d
BLAKE2b-256 102c61872ae8bc36c74624af91e5be29533c2ff2714615f3383103a1978ae57b

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphonesimulator.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphonesimulator.whl
Algorithm Hash digest
SHA256 bc06b9a8e8c0688c04aae5b61078c32f6a24706b25a4c9da1fb20308442c31f2
MD5 114704f0d21b0162c338b2a8f1bbce28
BLAKE2b-256 eea033e2cdce2fd3047391f798de3fb8e1a31eeaa3867a7f8b347e4955698b78

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphonesimulator.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphoneos.whl.

File metadata

File hashes

Hashes for slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphoneos.whl
Algorithm Hash digest
SHA256 c3f4be1635ae46559dfd2c1752fe7d44a0b180bf41fe986157af32c116c6d3e0
MD5 7dad5c57d0e795b1341ce4439ce69f13
BLAKE2b-256 8eeaf3bb6bdfa637eefed64fb7b8061ad8e797b2751c1c24696aaeddbb0ec929

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.12.1b1-cp310-abi3-ios_13_0_arm64_iphoneos.whl:

Publisher: upload_pypi.yaml on slint-ui/slint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page