0

I created a login gui using pyqt5 now. Then log in to the site using the request module. However, it takes a long time to log in, so I want to output a loading screen in the meantime. However, there is a method of using qthread? because I don't know how to perform parallel processing in pyqt5. What can I do?

Loading GUI

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDialog
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QMovie


class LoadingApp(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setFixedSize(200, 200)

        self.label_animation = QLabel(self)

        self.movie = QMovie('loading.gif')
        self.label_animation.setMovie(self.movie)

        timer = QTimer(self)
        self.startAnimation()
        timer.singleShot(3000, self.stopAnimation)

    def startAnimation(self):
        self.movie.start()

    def stopAnimation(self):
        self.movie.stop()
        self.close()

    def LoadingUI_exec(self):
        return super().exec_()

Main CODE >> I want to fill the tempform function

from PyQt5 import QtWidgets, QtCore
from loginUI import Ui_Form
from loadingUI import LoadingApp
import sys

class LoginApp(QtWidgets.QWidget, Ui_Form):
    def changeForm(self):
        if self.lr_button.isChecked():
            self.widget_2.hide()
            self.widget_3.show()
            self.lr_button.setText("Login")


        else:
            self.widget_2.show()
            self.widget_3.hide()
            self.lr_button.setText("Register")

    def tempform(self):

        #The login process is in a different script.
        #Therefore, I want to implement a code 
        #that simultaneously executes the login function and loading gui in this function.
        
        #The method call loadingGUI  >  LoadingApp().LoadingUI_exec()
        

    def exit(self):
        return
        sys.exit(0)

    def __init__(self):
        super(LoginApp, self).__init__()
        self.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        self.label.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=0, yOffset=0))
        self.label_2.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=0, yOffset=0))
        self.login_button.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=3, yOffset=3))
        self.register_button.setGraphicsEffect(QtWidgets.QGraphicsDropShadowEffect(blurRadius=25, xOffset=3, yOffset=3))

        self.widget_3.hide()
        self.lr_button.clicked.connect(self.changeForm)
        self.exit_button.clicked.connect(QtCore.QCoreApplication.instance().quit)
        self.login_button.clicked.connect(self.tempform)

#if __name__ == "__main__":
def LoginUI_exec():
    app = QtWidgets.QApplication(sys.argv)
    Form = LoginApp()
    Form.show()
    sys.exit(app.exec_())
조현성
  • 13
  • 3

1 Answers1

0

In Qt like in most GUIs all widgets live and perform in one thread, you cannot create or change or show widget from another thread. If you want to avoid unresponsive ui while blocking operation is performed, you should move that operation to another QThread using worker, or in your case you can use QNetworkRequest instead of requests, it already have asyncronous signal-slot interface.

mugiseyebrows
  • 3,205
  • 1
  • 13
  • 14
  • Yeah, I know, but I'm not sure how to write it. So, if I can execute the code one line at a time in an easier way, I'd like to assign only one line to a new process and then execute the code in the line below. – 조현성 Apr 01 '22 at 11:30