Tell me how to make the program run immediately, and not wait until the data is downloaded from the site? I need to first show the inscription "loading data ..." and then the title of the page ................ who can rewrite the code thanks in advance! Here is the code:

#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import (QWidget, QPushButton, QLabel,QInputDialog, QApplication) from PyQt5.QtGui import QIcon,QFont,QColor,QPalette,QPixmap,QBrush from PyQt5 import QtCore from urllib.request import urlopen from lxml import html import threading class Пример(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(10, 30, 250, 200) self.setWindowTitle('PyQt5') self.label = QLabel(self) self.label.setText(str("загрузка данных...")) self.label.setGeometry(0,0,188,20) self.label2=QLabel(self) self.label2.setGeometry(0,0,188,20) if p2.is_alive()!=False: self.label.hide() self.label2.setText(str(proc2())) self.label2.show() def proc(): app = QApplication(sys.argv) пример=Пример() пример.show() sys.exit(app.exec_()) def proc2(): страница=urlopen('http://gismeteo.ru/weather-rudniy-4629').read() parsed_body=html.fromstring(страница) заголовок=parsed_body.xpath('//title/text()')[0] return заголовок p1=threading.Thread(target=proc) p2=threading.Thread(target=proc2) p1.start() p2.start() p1.join() p2.join() 
  • Use threading.Thread or QNetworkAccessManager . The second is even preferable, since you use Qt. - Avernial
  • I'm a beginner . Can you write more about QNetworkAccessManager? - Alexander

1 answer 1

Option with QNetworkAcessManager . With this implementation, your application first starts up, then after 500ms a signal is sent to load the data. After the data is loaded, the loaded method is called in which the data will be processed and displayed.

 import sys from PyQt5 import Qt from lxml import html class Weather(Qt.QWidget): def __init__(self, parent=None): super().__init__(parent) self.nam = Qt.QNetworkAccessManager() self.initUI() Qt.QTimer.singleShot(500, self.on_timer) def initUI(self): self.setGeometry(10, 30, 250, 200) self.setWindowTitle('PyQt5') layout = Qt.QVBoxLayout(self) self.label = Qt.QLabel("Загрузка данных...") self.label.setGeometry(0, 0, 188, 20) self.label2 = Qt.QLabel() self.label2.setGeometry(0, 0, 188, 20) layout.addWidget(self.label) layout.addWidget(self.label2) def loaded(self): self.label.setText("Данные загружены!") page = self.reply.readAll().data().decode() parsed_body = html.fromstring(page) self.label2.setText(parsed_body.xpath('//title/text()')[0]) def on_error(self): Qt.QMessageBox.critical(self, 'Error', self.reply.errorString()) def on_timer(self): url = Qt.QUrl('https://gismeteo.ru/weather-rudniy-4629') self.nr = Qt.QNetworkRequest(url) self.reply = self.nam.get(self.nr) self.reply.finished.connect(self.loaded) self.reply.error.connect(self.on_error) if __name__ == '__main__': app = Qt.QApplication(sys.argv) w = Weather() w.show() sys.exit(app.exec_()) 

PS Do not write the code in Russian, get used immediately to use only English.