Skip to main content

a library bringing Qt GUIs together with ``async`` and ``await`` via Trio

Project description

Resources

Documentation

Read the Docs

Documentation

Chat

Gitter

Support chatroom

Forum

Discourse

Support forum

Issues

GitHub

Issues

Repository

GitHub

Repository

Tests

GitHub Actions

Tests

Coverage

Codecov

Test coverage

Distribution

PyPI

Latest distribution version
Supported Python versions
Supported Python interpreters

Introduction

Note:

This library is in early development. It works. It has tests. It has documentation. Expect breaking changes as we explore a clean API. By paying this price you get the privilege to provide feedback via GitHub issues to help shape our future. :]

The QTrio project’s goal is to bring the friendly concurrency of Trio using Python’s async and await syntax together with the GUI features of Qt to enable more correct code and a more pleasant developer experience. QTrio is permissively licensed to avoid introducing restrictions beyond those of the underlying Python Qt library you choose. Both PySide2 and PyQt5 are supported.

By enabling use of async and await it is possible in some cases to write related code more concisely and clearly than you would get with the signal and slot mechanisms of Qt concurrency. In this set of small examples we will allow the user to input their name then use that input to generate an output message. The user will be able to cancel the input to terminate the program early. In the first example we will do it in the form of a classic “hello” console program. Well, classic plus a bit of boilerplate to allow explicit testing without using special external tooling. Then second, the form of a general Qt program implementing this same activity. And finally, the QTrio way.

# A complete runnable source file with imports and helpers is available in
# either the documentation readme examples or in the repository under
# qtrio/examples/readme/console.py.

def main(
    input_file: typing.TextIO = sys.stdin, output_file: typing.TextIO = sys.stdout
) -> None:
    try:
        output_file.write("What is your name? ")
        output_file.flush()
        name = input_file.readline()[:-1]
        output_file.write(f"Hi {name}, welcome to the team!\n")
    except KeyboardInterrupt:
        pass

Nice and concise, including the cancellation via ctrl+c. This is because we can stay in one scope thus using both local variables and a try/except block. This kind of explodes when you shift into a classic Qt GUI setup.

# A complete runnable source file with imports and helpers is available in
# either the documentation readme examples or in the repository under
# qtrio/examples/readme/qt.py.

class Main:
    def __init__(
        self,
        application: QtWidgets.QApplication,
        input_dialog: typing.Optional[QtWidgets.QInputDialog] = None,
        output_dialog: typing.Optional[QtWidgets.QMessageBox] = None,
    ):
        self.application = application

        if input_dialog is None:  # pragma: no cover
            input_dialog = create_input()

        if output_dialog is None:  # pragma: no cover
            output_dialog = create_output()

        self.input_dialog = input_dialog
        self.output_dialog = output_dialog

    def setup(self) -> None:
        self.input_dialog.accepted.connect(self.input_accepted)
        self.input_dialog.rejected.connect(self.input_rejected)

        self.input_dialog.show()

    def input_accepted(self) -> None:
        name = self.input_dialog.textValue()

        self.output_dialog.setText(f"Hi {name}, welcome to the team!")

        self.output_dialog.finished.connect(self.output_finished)
        self.output_dialog.show()

    def input_rejected(self) -> None:
        self.application.quit()

    def output_finished(self) -> None:
        self.application.quit()

The third example, below, shows how using async and await allows us to return to the more concise and clear description of the sequenced activity. Most of the code is just setup for testability with only the last four lines really containing the activity.

# A complete runnable source file with imports and helpers is available in
# either the documentation readme examples or in the repository under
# qtrio/examples/readme/qtrio_example.py.

async def main(
    *,
    task_status: trio_typing.TaskStatus[Dialogs] = trio.TASK_STATUS_IGNORED,
) -> None:
    dialogs = Dialogs()
    task_status.started(dialogs)

    with contextlib.suppress(qtrio.UserCancelledError):
        name = await dialogs.input.wait()
        dialogs.output.text = f"Hi {name}, welcome to the team!"
        await dialogs.output.wait()

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

qtrio-0.7.0.tar.gz (60.4 kB view details)

Uploaded Source

Built Distribution

qtrio-0.7.0-py3-none-any.whl (53.3 kB view details)

Uploaded Python 3

File details

Details for the file qtrio-0.7.0.tar.gz.

File metadata

  • Download URL: qtrio-0.7.0.tar.gz
  • Upload date:
  • Size: 60.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for qtrio-0.7.0.tar.gz
Algorithm Hash digest
SHA256 98b3553598a057cbf053ed03f701f1be6e5b28647192b79e5a858763991cb974
MD5 f7a28bbc91975a13da70738d82f8537e
BLAKE2b-256 c8822298bfc9a9c03c3434cbd3800c0dc565d6062740bb864b73a649a71a38fc

See more details on using hashes here.

File details

Details for the file qtrio-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: qtrio-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 53.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for qtrio-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58803879e29fa4260f34c1ce4f0e4521fd25083e5145072bdda4e3b1b5cb99a8
MD5 916faf046cec62896f3b66d22fd61676
BLAKE2b-256 61f4efd92132d1a4771fa3c159efc37384a6e9583b8ad84175154854086df894

See more details on using hashes here.

Supported by

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