如何在 window 完全加载之前显示加载屏幕?

How can I make a loading screen appear before the window is fully loaded?

所以我的应用程序 window 似乎打开速度很慢,因为它必须在其中加载大约 400 个 PNG。我想让加载屏幕在完全加载之前出现。 window 代码类似于:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_Form3(object):
    def setupUi(self, Form3):

    #design choices

    self.scrollArea = QtWidgets.QScrollArea(self.frame) #a scroll area that contains 400 buttons
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
    self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 639, 40712))
    self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents_2)
    self.verticalLayout.setObjectName("verticalLayout")
    for i in range(400):  #this part generates all the buttons
        iplusone = i + 1
        bttnstring = "pushButton_" + str(iplusone)
        self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents_2)
        self.pushButton.setText('sometext')
        self.pushButton.setObjectName(bttnstring)
        self.verticalLayout.addWidget(self.pushButton)
    
    #more design choices

    def retranslateUi(self, Form3):
    _translate = QtCore.QCoreApplication.translate
    Form3.setWindowTitle(_translate("Form3", "App"))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    Form3 = QtWidgets.QWidget()
    ui = Ui_Form3()
    ui.setupUi(Form3)
    Form3.show()
    sys.exit(app.exec_())

我可以制作一个加载屏幕,当这个 window 应该打开并保持活动状态直到变量 i 达到值 400 时才会打开吗?如果没有,我还能做什么?

是的,你可以。

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    # loading widget
    app.loading = LoadingWidget()
    app.loading.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.FramelessWindowHint)  # make it always active
    app.loading.show()

    Form3 = QtWidgets.QWidget()
    ui = Ui_Form3()
    ui.setupUi(Form3)
    Form3.show()
    sys.exit(app.exec_())

然后,在加载图像的函数中的某处添加:

loading = QtWidgets.QApplication.instance().loading
loading.hide()

奖金。某种加载小部件:

class LoadingWindow(QWidget):
    def __init__(self, parent):
        super().__init__(parent)

        main_layout = QtWidgets.QHBoxLayout()
        self.text = QLabel("Loading, please wait")
        self.text.setStyleSheet("QLabel { color: white; }")

        main_layout.addWidget(self.text)

        self.setStyleSheet("background-color: #59595e")
        self.setLayout(main_layout)

    def show(self):
        # you can override this method to put your widget at center etc..
        super().show()