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 examples/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 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();
            }
        }
    }
}
  1. 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()
  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.

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.7.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.7.2a1-cp310-abi3-win_amd64.whl (11.6 MB view details)

Uploaded CPython 3.10+Windows x86-64

slint-1.7.2a1-cp310-abi3-manylinux_2_35_x86_64.whl (15.9 MB view details)

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

slint-1.7.2a1-cp310-abi3-manylinux_2_31_armv7l.whl (15.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARMv7l

slint-1.7.2a1-cp310-abi3-manylinux_2_31_aarch64.whl (15.4 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARM64

slint-1.7.2a1-cp310-abi3-macosx_11_0_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ x86-64

slint-1.7.2a1-cp310-abi3-macosx_11_0_arm64.whl (11.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for slint-1.7.2a1.tar.gz
Algorithm Hash digest
SHA256 aef80177aa18b40d775a0328ea1619afa3b573504831763acc9b47094fbd4368
MD5 8c753d59d37ab59f27df15465a78de0f
BLAKE2b-256 4bcb1fc9281d9349dcf2c8230c0f1e7b03a891b17d1c5bc0d1f8f387912b8412

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-win_amd64.whl.

File metadata

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

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2c411247f9a21a321d247d54f679e561a1e9d8cb46f83a018d4935d96c68aad3
MD5 81e678121b28aa808f730eb00b2a111b
BLAKE2b-256 c5d7da5d129dc00d09f03c5340bd0cbc21c9e254ca48a34ca193e3a94c14b3bb

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 eff03e93b90425823ea553fc7741208f086d8a511cfbba67ca301532e1bd4631
MD5 eae04efaec55e9d7f22d98df824d097d
BLAKE2b-256 69d4f035f6684b6d2d8c0496285cb63a236f3d50bbee95b9e841248cdd182037

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-manylinux_2_31_armv7l.whl.

File metadata

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-manylinux_2_31_armv7l.whl
Algorithm Hash digest
SHA256 10aab392089b4ff933ab94571efc436b991d1e46a9a6023d6f9ca6eb256cd417
MD5 441c26fc149250cb9ad72c90cd9e9590
BLAKE2b-256 eb59a96be2a257eee2cc83ec943ef725068ed505f4b2b5488b4484b91871bdc2

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 caecd5baa3c925651cb4e4b7a313798859848bf7b050ee27ffd325a0456a1ea4
MD5 8baa00e2d35608363de7ec2752f463cd
BLAKE2b-256 ad5a0e66ad978bd0464ad36efb071164804eb23bb6035910db945498e467f5f9

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 7d93e151f9895f6b0a0f20f7a3f77adead313f989ae9b478bccf96f8709f67ec
MD5 57c620b5b91b11cce3a127677e9f563f
BLAKE2b-256 6caf206b5a0d5b8e266b604b0f0e5ca0a1e6cf979afd6a25d2ddeae22d216627

See more details on using hashes here.

File details

Details for the file slint-1.7.2a1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for slint-1.7.2a1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 38b9ea7e48d2e8953c01475271027fca51d2f6e9c0231b4357b6e9220c19ef41
MD5 9d18ecc50cb8490e793328329ebbcd4d
BLAKE2b-256 5be2c7fafd30b0296cee48d8dae3006b5ca9d556b0d2c14205b7c0a01abb4f16

See more details on using hashes here.

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