Skip to content

Commit

Permalink
Python: Add a Python version of the Printer Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed Mar 7, 2024
1 parent ec34f6a commit d208319
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/printerdemo/python/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: MIT

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
slint = { file = "../../../api/python", editable = true }

[dev-packages]

[requires]
python_version = "3"
23 changes: 23 additions & 0 deletions examples/printerdemo/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->

# Intro

This is the Python version of the Slint Printer Demo.

# Prerequisites

* [Python 3](https://python.org/)
* [pip](https://pypi.org/project/pip/)
* [Pipenv](https://pipenv.pypa.io/en/latest/installation.html#installing-pipenv)

# Setup

```bash
pipenv update
```

# Run

```bash
pipenv run python main.py
```
62 changes: 62 additions & 0 deletions examples/printerdemo/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: MIT

# autopep8: off
from datetime import timedelta, datetime
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "ui"))
from slint import Color, ListModel, Timer, TimerMode
import printerdemo_slint
# autopep8: on


class MainWindow(printerdemo_slint.MainWindow):
def __init__(self):
super().__init__()
self.ink_levels = ListModel([
{"color": Color("#0ff"), "level": 0.4},
{"color": Color("#ff0"), "level": 0.2},
{"color": Color("#f0f"), "level": 0.5},
{"color": Color("#000"), "level": 0.8},
])
# Copy the read-only mock data from the UI into a mutable ListModel
self.printer_queue = ListModel(self.PrinterQueue.printer_queue)
self.PrinterQueue.printer_queue = self.printer_queue
self.PrinterQueue.start_job = self.push_job
self.PrinterQueue.cancel_job = self.remove_job
self.print_progress_timer = Timer()
self.print_progress_timer.start(
TimerMode.Repeated, timedelta(seconds=1), self.update_jobs)

self.quit = lambda: self.hide()

def push_job(self, title):
self.printer_queue.append({
"status": "waiting",
"progress": 0,
"title": title,
"owner": "Me",
"pages": 1,
"size": "100kB",
"submission_date": str(datetime.now()),
})

def remove_job(self, index):
del self.printer_queue[index]

def update_jobs(self):
if len(self.printer_queue) <= 0:
return
top_item = self.printer_queue[0]
top_item["progress"] += 1
if top_item["progress"] >= 100:
del self.printer_queue[0]
if len(self.printer_queue) == 0:
return
top_item = self.printer_queue[0]
self.printer_queue[0] = top_item


main_window = MainWindow()
main_window.run()

0 comments on commit d208319

Please sign in to comment.