Skip to main content

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 demos/printerdemo/python
pipenv update
pipenv run python main.py

Quick Start

  1. Add Slint Python Package Index to your Python project: pipenv install slint
  2. 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 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:

  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, which contains classes for each exported component that inherits Window. If the file name contains a dash, like app-window.slint, an attribute lookup for app_window will first try 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:

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


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.9.2a1.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

slint-1.9.2a1-cp310-abi3-win_amd64.whl (12.1 MB view details)

Uploaded CPython 3.10+Windows x86-64

slint-1.9.2a1-cp310-abi3-manylinux_2_35_x86_64.whl (16.6 MB view details)

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

slint-1.9.2a1-cp310-abi3-manylinux_2_31_armv7l.whl (15.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARMv7l

slint-1.9.2a1-cp310-abi3-manylinux_2_31_aarch64.whl (15.9 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARM64

slint-1.9.2a1-cp310-abi3-macosx_11_0_x86_64.whl (12.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ x86-64

slint-1.9.2a1-cp310-abi3-macosx_11_0_arm64.whl (11.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file slint-1.9.2a1.tar.gz.

File metadata

  • Download URL: slint-1.9.2a1.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for slint-1.9.2a1.tar.gz
Algorithm Hash digest
SHA256 8a4fe8437f7dd37c3555a08ba9879b0bc83b67dde894143ce9e60e6a9bad3b04
MD5 7fc10f1803cc3e95669edb59c39f4588
BLAKE2b-256 e667f5277477ddfe5cc9478d563299f61847dfe6ce14023d7a82b7a319208a19

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1.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.9.2a1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: slint-1.9.2a1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.1 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 100693ec2f3d342d04f135a9f073d5686eb2f4f5eaba75c8039e7b2b0cd6f815
MD5 4016d13cba1d54efc38e00e5ba530218
BLAKE2b-256 91389700ec975fa32cf1767e57b1fe02962fa3fe2d54ea3a474a644612ccf2b8

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.9.2a1-cp310-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 ac298004c22bbfb2b09894c64ece2717af4f3b38194269330089cc10f6bdee9f
MD5 9037b4cfc86b5bb137c079b7d1523e6f
BLAKE2b-256 b6e96b6ae61e33e1468d2958414cc00930c0475e7ab4c7f83294e7e9e69fc214

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.9.2a1-cp310-abi3-manylinux_2_31_armv7l.whl.

File metadata

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-manylinux_2_31_armv7l.whl
Algorithm Hash digest
SHA256 f575e393ea309a0ef1b080d3bcb3efb5e454cafbf5ba93d0a1cd94043ab47fbc
MD5 6b7b825366d5ffdef352564670eeb9bd
BLAKE2b-256 29e61ce6c72ad8daea307b9e4f4b308d3fb08d9c52f6fbeb9cd68b00230a3cb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.9.2a1-cp310-abi3-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 6af53638b337a7c9720e2eb0a916b0f27537e76c4361764d7c8c2a0df8fc107e
MD5 e2b5d660a2a855eacffc9300038fe67f
BLAKE2b-256 092ab992ef959acc4d6213a7f632f608cdff6a9254b25125fbe6917557d63e77

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.9.2a1-cp310-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a7a90e644d7bd76e11405914a5ab08baa1f923f6f4760658e4d51b278e1ac270
MD5 5c674b162263d88010dfea35ab6ae7d1
BLAKE2b-256 acab7693aad5be978093a0cc7b0501717d1c2daf637a9490fa70bb2907a63758

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.9.2a1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for slint-1.9.2a1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 496c8f53799e781ffe8da8db89a076fa750151012efe923d6feba077ff1de378
MD5 6022d64d627622fd4797fbaafe05cb0b
BLAKE2b-256 73ab75ffab4dc5ba816530fb70e8e2fd9edfecb73c4e0ae74abf89b6e0309393

See more details on using hashes here.

Provenance

The following attestation bundles were made for slint-1.9.2a1-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.

Supported by

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