在函数执行期间在 PyQt5 中显示启动画面

Display splash-screen in PyQt5 during function execution

我有一个程序,我用 PyQt5 显示 GUI。从我的 MainWindow 我打开一个 QWidget 从中启动一个功能。虽然此函数是 运行,但应该会出现 SplashScreen。一旦函数完成,SplashScreen 应该再次关闭。我试图从函数内部启动 SplashScreen 。但是,SplashScreen 没有出现。

我使用了以下代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QProgressBar, QLabel, QFrame, QMainWindow, QVBoxLayout, QPushButton
from PyQt5.QtCore import Qt
import time

class Function:
    def loading(self):
        self.screen = SplashScreen()
        self.screen.show()
        time.sleep(10)
        self.screen.close()

class SplashScreen(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Spash Screen Example')
        self.setFixedSize(1100, 500)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlag(Qt.WindowStaysOnTopHint)
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()
        self.setLayout(layout)

        self.frame = QFrame()
        layout.addWidget(self.frame)

        self.labelTitle = QLabel(self.frame)
        self.labelTitle.setObjectName('LabelTitle')

        # center labels
        self.labelTitle.resize(self.width() - 10, 150)
        self.labelTitle.move(0, 40)  # x, y
        self.labelTitle.setText('Splash Screen')
        self.labelTitle.setAlignment(Qt.AlignCenter)

        self.labelDescription = QLabel(self.frame)
        self.labelDescription.resize(self.width() - 10, 50)
        self.labelDescription.move(0, self.labelTitle.height())
        self.labelDescription.setObjectName('LabelDesc')
        self.labelDescription.setText('<strong>Working on Task #1</strong>')
        self.labelDescription.setAlignment(Qt.AlignCenter)

        self.progressBar = QProgressBar(self.frame)
        self.progressBar.resize(self.width() - 200 - 10, 50)
        self.progressBar.move(100, self.labelDescription.y() + 130)
        self.progressBar.setAlignment(Qt.AlignCenter)
        self.progressBar.setFormat('%p%')
        self.progressBar.setTextVisible(True)
        self.progressBar.setRange(0, 100)
        self.progressBar.setValue(20)

        self.labelLoading = QLabel(self.frame)
        self.labelLoading.resize(self.width() - 10, 50)
        self.labelLoading.move(0, self.progressBar.y() + 70)
        self.labelLoading.setObjectName('LabelLoading')
        self.labelLoading.setAlignment(Qt.AlignCenter)
        self.labelLoading.setText('loading...')

        self.setStyleSheet('''
                        #LabelTitle {
                            font-size: 60px;
                            color: #93deed;
                        }

                        #LabelDesc {
                            font-size: 30px;
                            color: #c2ced1;
                        }

                        #LabelLoading {
                            font-size: 30px;
                            color: #e8e8eb;
                        }

                        QFrame {
                            background-color: #2F4454;
                            color: rgb(220, 220, 220);
                        }

                        QProgressBar {
                            background-color: #DA7B93;
                            color: rgb(200, 200, 200);
                            border-style: none;
                            border-radius: 10px;
                            text-align: center;
                            font-size: 30px;
                        }

                        QProgressBar::chunk {
                            border-radius: 10px;
                            background-color: qlineargradient(spread:pad x1:0, x2:1, y1:0.511364, y2:0.523, stop:0 #1C3334, stop:1 #376E6F);
                        }
                    ''')

class Window(QWidget):
    def __init__(self):
        super().__init__()
        layout_window = QVBoxLayout()
        self.setLayout(layout_window)
        self.button = QPushButton("Open", self)
        layout_window.addWidget(self.button)
        self.button.clicked.connect(self.open)

    def open(self):
        self.function = Function()
        self.function.loading()


class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.window_width, self.window_height = 1200, 800
        self.setMinimumSize(self.window_width, self.window_height)

        layout = QVBoxLayout()
        self.setLayout(layout)
        self.button = QPushButton("Open", self)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.open)

    def open(self):
        self.window = Window()
        self.window.show()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()

    try:
        sys.exit(app.exec_())
    except SystemExit:
        print('Closing Window...')

我用time.sleep表示了函数(在原始程序中发生了一些耗时的数据处理)。当我省略 close-function 时,'SplashScreen' 在 time.sleep 完成后打开。

time.sleep 将阻塞 event-loop,因此任何 GUI-related 操作都将被挂起,直到它完成。因此,您需要使用 processEvents:

显式强制处理任何未决事件
class Function:
    def loading(self):
        self.screen = SplashScreen()
        self.screen.show()
        for i in range(100):
            self.screen.progressBar.setValue(i)
            QApplication.processEvents()
            time.sleep(0.1)
        self.screen.close()

或者将time-consuming任务移动到工作线程,然后使用信号更新GUI:

from PyQt5.QtCore import pyqtSignal, QThread

class Thread(QThread):
    progressChanged = pyqtSignal(int)

    def run(self):
        for i in range(100):
            QThread.msleep(100)
            self.progressChanged.emit(i)

class Function:
    def loading(self):
        self.screen = SplashScreen()
        self.screen.show()
        self.thread = Thread()
        self.thread.progressChanged.connect(
            self.screen.progressBar.setValue)
        self.thread.finished.connect(self.screen.close)
        self.thread.start()

通常建议采用后一种方法,但如果无法将任务分解为离散的步骤,则可能需要改用 multiprocessing