1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How does QtAsyncio.run() know what to run?

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 00:22.

  1. Stack

    Stack Membro Participativo

    Full code for this example is below. I'm more interested in this part:

    if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()

    QtAsyncio.run()


    How does QtAsyncio.run() know what to run? nothing is passed to it.

    # Copyright (C) 2022 The Qt Company Ltd.
    # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget)

    import PySide6.QtAsyncio as QtAsyncio

    import asyncio
    import sys


    class MainWindow(QMainWindow):

    def __init__(self):
    super().__init__()

    widget = QWidget()
    self.setCentralWidget(widget)

    layout = QVBoxLayout(widget)

    self.text = QLabel("The answer is 42.")
    layout.addWidget(self.text, alignment=Qt.AlignmentFlag.AlignCenter)

    async_trigger = QPushButton(text="What is the question?")
    async_trigger.clicked.connect(lambda: asyncio.ensure_future(self.set_text()))
    layout.addWidget(async_trigger, alignment=Qt.AlignmentFlag.AlignCenter)

    async def set_text(self):
    await asyncio.sleep(1)
    self.text.setText("What do you get if you multiply six by nine?")


    if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()

    QtAsyncio.run()

    Continue reading...

Compartilhe esta Página