PYQT5 加载屏幕直到主图形用户界面初始化(冻结屏幕)

问题描述 投票:0回答:3

我想实现一个加载屏幕,该屏幕将显示在主界面或应用程序 gui 前面

Main_Window
。 加载屏幕
Load_Window
应该包含一个 gif 图像和一个持续显示主界面
Main_Window
已初始化多远的进度条。 主图形用户界面完全初始化后,加载屏幕应该消失并显示主图形用户界面。

ProcessBar PyQt5

到目前为止,我已经尝试了 QThread、QSplashscreen 和 app.processEvent() 方法,但是加载屏幕被冻结了,因为 pyqt5 事件处理程序没有运行。

frozen  gui

在下面的代码示例中,我已将问题作为示例显示。代码是可执行的!

Main_Window
中的for循环代表启动配置文件、线程和初始化GUI等

如何在另一个GUI 正在初始化时显示和更新加载屏幕? 我怎样才能防止这种影响?

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QDialog, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QThread
from PyQt5.QtCore import QSize

class Main_Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 480))
        self.setWindowTitle("Main Window")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)

        gridLayout = QGridLayout(self)
        centralWidget.setLayout(gridLayout)

        title = QLabel("I am the main window", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        gridLayout.addWidget(title, 0, 0)

        #   Running the loading screen in own thread, but the event handler is the same :(
        # self.Thread = Thread_Load()
        # self.Thread.start()


        #   Call event handler to process the queue
        #app.processEvents()
        self.show()

        for i in range(0,1000000):
            print(i)
class Load_Window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setGeometry(0,0,500,500)

        self.setWindowTitle("Load Window")

        title = QLabel("I am the Load window", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        title.move(200,200)
        self.show()

# class Thread_Load(QThread):
#     def __init__(self, parent=None):
#         QThread.__init__(self, parent)
#         Load_Window()
#     def run(self):
#         #   Call event handler to process the queue
#         while True:
#             app.processEvents()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    LoadWin = Load_Window()
    MainWin = Main_Window()
    sys.exit(app.exec_())
python python-3.x user-interface pyqt pyqt5
3个回答
1
投票

创建一个辅助函数

run()
来完成您的所有工作并随时更改两个全局标志的值。我更喜欢
threading
模块。但是你也可以用
QThread
找到类似的实现。

import sys, time
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QDialog, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QThread
from PyQt5.QtCore import QSize

alive, Progress = True, 0

class Main_Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(640, 480))
        self.setWindowTitle("Main Window")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)

        gridLayout = QGridLayout(self)
        centralWidget.setLayout(gridLayout)

        title = QLabel("I am the main window", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        gridLayout.addWidget(title, 0, 0)

        #   Running the loading screen in own thread, but the event handler is the same :(
        # self.Thread = Thread_Load()
        # self.Thread.start()


        #   Call event handler to process the queue
        #app.processEvents()
        self.show()

        #for i in range(0,1000000):
        #    print(i)

class Load_Window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setGeometry(0,0,500,500)

        self.setWindowTitle("Load Window")

        title = QLabel("I am the Load window", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        title.move(200,200)
        self.show()

def run():
    global open, Progress
    time.sleep(3)
    #Do your stuff here, like change the value of Progress as you go through your for loop
    alive=False;

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    LoadWin = Load_Window()
    import threading
    thread1 = threading.Thread(target = run)
    thread1.start()
    while alive:
        time.sleep(.05)
        #Set progressbar value as the value of Progress.
        app.processEvents()
    LoadWin.close()
    MainWin=Main_Window()
    sys.exit(app.exec_())

1
投票

答案是@musicamante留下的评论

如果你只是想解冻你的 LoadWin,你只需要在 LoadWin 之后放置 processEvents()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    LoadWin = Load_Window()
    app.processEvents()
    MainWin = Main_Window()
    LoadWin.close()
    sys.exit(app.exec_())

如果您想在尝试加载主窗口时移动加载屏幕,您可以向 QDialog 发送 pyqtSignal 以指示里程碑已实现。

import sys, time
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QDialog, QLabel, QGridLayout, QWidget, QProgressBar
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtCore import QSize

class Main_Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Progress = 10
        LoadWin.progressUpdate(Progress)
        time.sleep(2)
        self.setMinimumSize(QSize(640, 480))
        self.setWindowTitle("Main Window")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)
        Progress = 30
        LoadWin.progressUpdate(Progress)
        time.sleep(2)

        gridLayout = QGridLayout(self)
        centralWidget.setLayout(gridLayout)
        Progress = 50
        LoadWin.progressUpdate(Progress)
        time.sleep(2)

        title = QLabel("I am the main window", self)
        title.setAlignment(QtCore.Qt.AlignCenter)
        gridLayout.addWidget(title, 0, 0)

        #   Running the loading screen in own thread, but the event handler is the same :(
        # self.Thread = Thread_Load()
        # self.Thread.start()


        #   Call event handler to process the queue
        #app.processEvents()
        Progress = 100
        LoadWin.progressUpdate(Progress)
        self.show()
        LoadWin.close()

class Load_Window(QDialog):
    
    def __init__(self):
        QDialog.__init__(self)
        self.setGeometry(0,0,500,500)
        self.setWindowTitle("Load Window")

        title = QLabel("I am the Load window", self)
        self.pbar = QProgressBar(self)
        
        self.pbar.setGeometry(30, 40, 200, 25)
        
        title.setAlignment(QtCore.Qt.AlignCenter)
        title.move(200,200)
        self.show()

    def progressUpdate(self, progress):
        self.pbar.setValue(progress)

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

    MainWin=Main_Window()
    sys.exit(app.exec_())

0
投票

当类位于单独的 Main_Window.py 文件中时,如何在 Main_Window 类中看到 LoadWin 对象

© www.soinside.com 2019 - 2024. All rights reserved.