所以我的应用程序窗口似乎打开很慢,因为它必须在其中加载大约 400 个 PNG。我想让加载屏幕在完全加载之前出现。窗口代码如下所示:
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_())
我可以制作一个加载屏幕,当这个窗口应该打开并保持活动状态直到变量 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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句