跳转到主要内容

一个通过Trio将Qt GUI与`async`和`await`结合起来的库

项目描述

资源

文档

Read the Docs

Documentation

聊天

Gitter

Support chatroom

论坛

Discourse

Support forum

问题

GitHub

Issues

仓库

GitHub

Repository

测试

GitHub Actions

Tests

覆盖率

Codecov

Test coverage

分发

PyPI

Latest distribution version
Supported Python versions
Supported Python interpreters

简介

注意

这个库处于早期开发阶段。它可行。它有测试。它有文档。在探索干净的API时,请期待破坏性更改。通过支付这个代价,您将获得通过GitHub问题提供反馈的特权,以帮助我们塑造未来。 :]

QTrio项目的目标是结合Python的asyncawait语法,以及Qt的GUI功能,以实现更正确的代码和更愉悦的开发体验。QTrio采用许可协议,以避免引入超出您选择的底层Python Qt库限制。PySide2和PyQt5都受到支持。

通过启用使用asyncawait,在某些情况下,可以比使用Qt并发的信号和槽机制编写更简洁、更清晰的代码。在这个小型示例集中,我们将允许用户输入他们的名字,然后使用该输入生成输出消息。用户将能够取消输入以提前终止程序。在第一个示例中,我们将以经典的“hello”控制台程序的形式完成它。好吧,经典加上一点样板代码,以便在不使用特殊外部工具的情况下进行显式测试。然后是第二个,以实现相同活动的通用Qt程序的形式。最后,是QTrio的方式。

# 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

简洁且易于取消(使用ctrl+c)。这是因为我们可以保持在一个作用域内,因此可以使用局部变量和try/except块。这种类型在转换到经典的Qt GUI设置时会爆炸。

# 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()

下面的第三个示例展示了如何使用asyncawait使我们能够返回到更简洁、更清晰的顺序活动描述。大部分代码只是为了测试而设置,只有最后四行真正包含活动。

# 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()

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源分布

qtrio-0.7.0.tar.gz (60.4 kB 查看哈希值)

上传时间:

构建分布

qtrio-0.7.0-py3-none-any.whl (53.3 kB 查看哈希值)

上传时间: Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面