python asyncio - setText in a function before await not working - Stack Overflow

when running this code:from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit

when running this code:

from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
from PySide6.QtGui import QIcon, QFont, QValidator
from PySide6.QtCore import QSize, QPoint, Qt

import PySide6.QtAsyncio as QtAsyncio

import asyncio
import requests
import time

class AppWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('METAR Tool')
        self.setWindowIcon(QIcon('cloud.ico'))
        self.setFixedSize(QSize(500, 300))
        self.setStyleSheet("""
            QPushButton {
                color: white;
                background-color: #42baff;
                border-radius: 5px;
            }
            QPushButton:hover, QPushButton:disabled {
                background-color: #3ca8e6;
            }
            QPushButton:pressed {
                background-color: #005180;                
            }
        """)

        self.main_font = QFont('Aptos', 18)
        self.main_font.setLetterSpacing(QFont.SpacingType(1), 1)

        self.input = QLineEdit(parent=self)
        self.input.setFont(self.main_font)
        self.input.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.input.setFixedSize(QSize(400, 60))
        self.input.setPlaceholderText('ICAO')
        self.input.move(QPoint(50, 60))
        
        self.button = QPushButton(parent=self, text='Fetch METAR')
        self.button.setFont(self.main_font)
        self.button.setFixedSize(QSize(400, 60))
        self.button.move(QPoint(50, 180))
        self.button.clicked.connect(lambda: asyncio.ensure_future(self.fetch_process('LKPR')))

    async def fetch_process(self, icao):
        self.setDisabled(True)
        self.button.setText('Fetching...')
        self.response = await self.fetch_request(icao)
        self.setDisabled(False)

    async def fetch_request(self, icao):
        return requests.get(f'={icao}&format=json&taf=false&hours=0.5').json()

class AppCore():
    def __init__(self):
        self.app = QApplication([])
        self.app.setStyle('Breeze')

        self.window = AppWindow()
        self.window.show()

    def run(self):
        QtAsyncio.run(handle_sigint=True)

AppCore().run()

the text on the self.button only changes after the data is fetched from the API. I want it to change the text when clicked, then wait, until the data is fetched and then do something else. How do I do that ?

I found the example of running an async function on clicking a button at .html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744397106a4572197.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信