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
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/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).

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

Uploaded CPython 3.10+Windows x86-64

slint-1.7.1a1-cp310-abi3-manylinux_2_35_x86_64.whl (16.0 MB view details)

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

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

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARMv7l

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

Uploaded CPython 3.10+manylinux: glibc 2.31+ ARM64

slint-1.7.1a1-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.1a1-cp310-abi3-macosx_11_0_arm64.whl (11.1 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for slint-1.7.1a1.tar.gz
Algorithm Hash digest
SHA256 92b59afe252357d4f483f7011b5ca3007e7b8ba48d06dd5f39fb557ed7986544
MD5 beba4fa08815f4e39f742cf0c3e06aee
BLAKE2b-256 a843e87538c50284d489a974ae420a22c65b7b94cb120fbd5b3418c494115573

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slint-1.7.1a1-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.4

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d00167d4c64f35cddc822812ce999b36ee77c955da00c211bcd7fc4dfa1a9208
MD5 0a33128aafc76d0ad887c58a81e334d9
BLAKE2b-256 c6f1c5d19a4bc4d3a3c8a02dd630988bd48e3e636039015e00fb0342b24d5cb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 5ebd423dba3ab891bddad0f910f1f3e5bf0b90b3fdeefcc0ad6e9240e60dcac8
MD5 306ed3e95aa86b1841c9709ba1f6812b
BLAKE2b-256 e3cd44236a293a46b608dffeb6efcd6eb5a4365e89a6a70f777e4140dd3506d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-manylinux_2_31_armv7l.whl
Algorithm Hash digest
SHA256 6b81327a650059cc1f90e616bc41e749133cd0ab6d15833a177c125d99b0be31
MD5 e690407e157dad2b698b3cdcdf7ee047
BLAKE2b-256 d7ac931c90cf4184f4a3a3ec9594d1c394fae649cf121bd3bdf6ee0165b20587

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 8ecadb45dda4d554fadbd7b3d01a7b67afeb77cebc726da4b6a421d0a1d72fd8
MD5 190cf8a8ae3357a121c3fcb9ad35226d
BLAKE2b-256 95d5757fef8179201f802728a8dffa9b9281fe73e6de9b39122adfcb49a84aeb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 834ff44f82eb8191a4da7e035e9a33f51140439aa2c5f1b7162a01194207ef98
MD5 74c6b9e58217e6965f49e7e29b1ed6bc
BLAKE2b-256 d7618dd7cc9604b82ba2f3d82774ce3849ac7ad7befc39d553f394f7e28d999e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slint-1.7.1a1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f5e6f5d6a34608f7dedd85e0107c6525294d1855f1f0ec84cad2d276d032d566
MD5 5d142ddbf6fb884cf1e2ebb549254e56
BLAKE2b-256 441ed7c26d54b49d0f954f584d34430c7c44ccf19b45e91f1f1d6268cf2313cf

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