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 from the Git development branch to your Python project: pipenv install "git+https://github.com/slint-ui/slint#subdirectory=api/python&egg=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
import appwindow_slint

class App(appwindow_slint.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:

ui.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("ui.slint"). The returned object is a namespace, that provides the MainWindow class:

    import slint
    components = slint.load_file("ui.slint")
    main_window = components.MainWindow()
    
  2. Import the .slint file as module by treating it like a Python module where the .slint extension is replaced with _slint:

    import slint # needs to come first
    from ui_slint import MainWindow
    main_window = MainWindow()
    

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


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.6.0a5.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.6.0a5-cp310-abi3-win_amd64.whl (3.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

slint-1.6.0a5-cp310-abi3-manylinux_2_35_x86_64.whl (6.0 MB view details)

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

slint-1.6.0a5-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (6.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

slint-1.6.0a5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

slint-1.6.0a5-cp310-abi3-macosx_11_0_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.10+macOS 11.0+ x86-64

slint-1.6.0a5-cp310-abi3-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file slint-1.6.0a5.tar.gz.

File metadata

  • Download URL: slint-1.6.0a5.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for slint-1.6.0a5.tar.gz
Algorithm Hash digest
SHA256 e6abc98af024915fc1b79f3a1fb0b6a2844f014b466329738eac73b452160326
MD5 0f3e0eaed21e8df98844bf0f244219da
BLAKE2b-256 2f9fd987a21d6a46a99e40d005b03327c2f939a954301bf3a898808088b1c6f1

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: slint-1.6.0a5-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.3 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 07addb792dbd46a9a6f8593a0db7a4c32dcf534ba5ec4c19a0a6250926be4df2
MD5 89d16e2ee052dd42aa1fc7790065782b
BLAKE2b-256 2a1b41c02fef0e667fd7aa35a167b3342416205a6d0967f4ed0ed4df7f3c6aea

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 0bbf74bd333ff859e9f73cda11f674dcc33d094b0af6d0d5c9ded78f80c85213
MD5 caaa45693b56030c3f1677977ccd59ca
BLAKE2b-256 bd36830a4b00cde300bc44b174985269bbf536098cd65f44ffdec9a8d94ee297

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 c664759a9676f22705586caf65827b07702bee30411d3e5d712367c28e5616fb
MD5 af9d22ec61cbf6d2d97eafb7774f3fda
BLAKE2b-256 2371b300a3c3c945b811de78ee31188afa88f54633265898669a64bf44d272bd

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7893cc36599f70e95930f7db33cdab980bd3dda2277b4f12091c3a8f27b51ce4
MD5 01362b4ffbd3deabad35246f7f360a46
BLAKE2b-256 d41e2d0286235a188d5ee3878e4291fafa6a26fd8edc176f4d21b4399a7a4fb2

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 f7357cb309d95fe6944465ddca09d576df2bda93c3ae6b5727f884d95b8b76e5
MD5 68278b95d96cef53cbd9b368d4c692fe
BLAKE2b-256 490ea3491fa38d7b2006797be009e5daa295840913764c99efea5cd87441c8ba

See more details on using hashes here.

File details

Details for the file slint-1.6.0a5-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for slint-1.6.0a5-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c10ca4b80404e5ed0b7ddcdb1b44523705f624733c8d4cb731a812f036058e3e
MD5 f3689bd233ca85d04040dca3ad279f27
BLAKE2b-256 79aaeb16aae6ed7220bf90b738b467b50c45cef587e1ae21048d9fd63789e573

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