一个通过Trio将Qt GUI与`async`和`await`结合起来的库
项目描述
资源
简介
- 注意
这个库处于早期开发阶段。它可行。它有测试。它有文档。在探索干净的API时,请期待破坏性更改。通过支付这个代价,您将获得通过GitHub问题提供反馈的特权,以帮助我们塑造未来。 :]
QTrio项目的目标是结合Python的async和await语法,以及Qt的GUI功能,以实现更正确的代码和更愉悦的开发体验。QTrio采用许可协议,以避免引入超出您选择的底层Python Qt库限制。PySide2和PyQt5都受到支持。
通过启用使用async和await,在某些情况下,可以比使用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()
下面的第三个示例展示了如何使用async和await使我们能够返回到更简洁、更清晰的顺序活动描述。大部分代码只是为了测试而设置,只有最后四行真正包含活动。
# 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 查看哈希值)
关闭
qtrio-0.7.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 98b3553598a057cbf053ed03f701f1be6e5b28647192b79e5a858763991cb974 |
|
MD5 | f7a28bbc91975a13da70738d82f8537e |
|
BLAKE2b-256 | c8822298bfc9a9c03c3434cbd3800c0dc565d6062740bb864b73a649a71a38fc |
关闭
qtrio-0.7.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 58803879e29fa4260f34c1ce4f0e4521fd25083e5145072bdda4e3b1b5cb99a8 |
|
MD5 | 916faf046cec62896f3b66d22fd61676 |
|
BLAKE2b-256 | 61f4efd92132d1a4771fa3c159efc37384a6e9583b8ad84175154854086df894 |