python - PYQT5 加载屏幕直到主 GUI 初始化(卡住屏幕)

标签 python python-3.x user-interface pyqt pyqt5

我想实现一个加载屏幕,该屏幕将显示在主或应用程序 GUI Main_Window 前面。 加载屏幕 Load_Window 应包含一个 gif 图像和一个进度条,该进度条持续显示主界面 Main_Window 已初始化的程度。 一旦主 GUI 完全初始化,加载屏幕就会消失并显示主 GUI。

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_())

最佳答案

创建一个工作函数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_())

关于python - PYQT5 加载屏幕直到主 GUI 初始化(卡住屏幕),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70798821/

相关文章:

python - 无法到达类参数中的函数

c# - 在 webbrowser 控件中模拟鼠标单击

android - 关于 Android 中按钮的非常简单的示例?

python - 操作系统改变目录结构

Python 从字典中删除值适合函数的项目?

python - Tensorflow 上的多维 RNN

简单游戏的 Python 经验和等级点

JDialog 关闭后 Java JComponent 不重新绘制

python - Tkinter 对话框 showinfo 循环错误

Python请求以utf-8编码的响应但无法解码