gpt4 book ai didi

python - 在调用 app.processEvents() 两次之前,qsplashscreen 无法正确显示

转载 作者:行者123 更新时间:2023-12-01 08:25:39 24 4
gpt4 key购买 nike

PyQT4.11
Python 2.7

我一直在尝试创建一个启动屏幕,以在选择特定选项卡时显示加载消息。当调用 app.processEvents() 时,我会显示一个灰色框而不是图像,但是如果我在调用第一个函数后添加第二个 app.processEvents()将显示正确的图像,直到启动完成后关闭。

我尝试过 sleep 计时器、重新绘制、更新、关闭和重新显示选项卡和选项卡小部件,但结果好坏参半,在极少数情况下它会起作用,但更多时候不会。任何人都可以阐明我所缺少的东西吗?

import sys
from PyQt4 import QtCore, QtGui

class splashTest(QtGui.QDialog):
def __init__(self, parent=None):
super(splashTest, self).__init__(parent)

self.setGeometry(000,000,800,400)
self.tabWidget = QtGui.QTabWidget(self)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 500, 500))
self.tabWidget.setObjectName("tabWidget")
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.tab3 = QtGui.QWidget()

self.tabWidget.addTab(self.tab1, "tab1")
self.tabWidget.addTab(self.tab2, "tab2")
self.tabWidget.addTab(self.tab3, "Click Me")

self.tabWidget.setCurrentIndex(1)

self.tabWidget.currentChanged.connect(self.whichTabIsSelected)


def whichTabIsSelected(self):
if self.tabWidget.currentIndex() == 2:
splash_pix = QtGui.QPixmap('splash.png')
splash = QtGui.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
app.processEvents()
self.timeConsumingFunction()
app.processEvents()
self.timeConsumingFunction()

splash.finish(self.tabWidget)

else:
pass

def timeConsumingFunction(self):
b = 0
for a in range(100000000):
b += a
print (b)


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = splashTest()
myapp.show()
sys.exit(app.exec_())

最佳答案

问题是您在主线程中运行繁重的任务,阻塞了 GUI 的事件循环,这会导致窗口卡住,并且您正在使用 QApplication::processEvents()强制更新,正确的解决方案是在另一个线程上执行该任务。

import sys
from PyQt4 import QtCore, QtGui
import threading

class splashTest(QtGui.QDialog):
finished = QtCore.pyqtSignal()

def __init__(self, parent=None):
super(splashTest, self).__init__(parent)

self.setGeometry(000,000,800,400)
self.tabWidget = QtGui.QTabWidget(self)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 500, 500))
self.tabWidget.setObjectName("tabWidget")
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.tab3 = QtGui.QWidget()

self.tabWidget.addTab(self.tab1, "tab1")
self.tabWidget.addTab(self.tab2, "tab2")
self.tabWidget.addTab(self.tab3, "Click Me")

self.tabWidget.setCurrentIndex(1)
self.tabWidget.currentChanged.connect(self.whichTabIsSelected)

@QtCore.pyqtSlot(int)
def whichTabIsSelected(self, index):
if index == 2:
splash_pix = QtGui.QPixmap('splash.png')
splash = QtGui.QSplashScreen(self, splash_pix, QtCore.Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
for _ in range(2):
loop = QtCore.QEventLoop()
self.finished.connect(loop.quit)
threading.Thread(target=self.timeConsumingFunction).start()
loop.exec_()
splash.finish(self.tabWidget)
splash.close()

def timeConsumingFunction(self):
b = 0
for a in range(100000000):
b += a
print (b)
self.finished.emit()


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = splashTest()
myapp.show()
sys.exit(app.exec_())

关于python - 在调用 app.processEvents() 两次之前,qsplashscreen 无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54267692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com