如何更改启动画面内的图像
How to change the image inside a Splash Screen
我正在开发一个应用程序,在启动时需要检查各种事情,例如是否找到模块、与微控制器的连接是否正常等。所以我决定制作一个启动画面,其中显示每个步骤一个消息。我的想法是制作几张图片,上面写着 ('Loading modules...')、('Checking connection...'),并在每次 运行 时更改它们。下面的代码是我想到的:
images = ["path1", "path2", ...]
app = QApplication(sys.argv)
for i in images:
pixmap = QPixmap(i)
splash = QSplashScreen(pixmap)
splash.show()
QTimer.singleShot(3000, splash.close) #the timer is just to pretend some process is running
app.processEvents()
app.exec_()
此代码只显示最后一张图片,而不是每张图片间隔 3 秒。关于如何解决这个问题的任何线索?
您必须创建指示状态和更改图像的信号:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Manager(QtCore.QObject):
"""Class that simulates the loading steps"""
started = QtCore.pyqtSignal()
loaded_modules = QtCore.pyqtSignal()
connected = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
def start(self):
self.started.emit()
for i, step in enumerate((self.loaded_modules, self.connect, self.finish), 1):
QtCore.QTimer.singleShot(i * 2000, step)
def load_modules(self):
self.loaded_modules.emit()
def connect(self):
self.connected.emit()
def finish(self):
self.finished.emit()
class SplashScreen(QtWidgets.QSplashScreen):
@QtCore.pyqtSlot()
def handle_started(self):
self.show()
self.setPixmap(QtGui.QPixmap("started.png"))
@QtCore.pyqtSlot()
def handle_loaded_modules(self):
self.setPixmap(QtGui.QPixmap("loaded_modules.png"))
@QtCore.pyqtSlot()
def handle_connected(self):
self.setPixmap(QtGui.QPixmap("connected.png"))
@QtCore.pyqtSlot()
def handle_finished(self):
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
w.resize(640, 480)
manager = Manager()
splash_screen = SplashScreen()
manager.started.connect(splash_screen.handle_started)
manager.loaded_modules.connect(splash_screen.handle_loaded_modules)
manager.connected.connect(splash_screen.handle_connected)
manager.finished.connect(splash_screen.handle_finished)
manager.finished.connect(w.show)
manager.start()
app.exec_()
if __name__ == "__main__":
main()
我正在开发一个应用程序,在启动时需要检查各种事情,例如是否找到模块、与微控制器的连接是否正常等。所以我决定制作一个启动画面,其中显示每个步骤一个消息。我的想法是制作几张图片,上面写着 ('Loading modules...')、('Checking connection...'),并在每次 运行 时更改它们。下面的代码是我想到的:
images = ["path1", "path2", ...]
app = QApplication(sys.argv)
for i in images:
pixmap = QPixmap(i)
splash = QSplashScreen(pixmap)
splash.show()
QTimer.singleShot(3000, splash.close) #the timer is just to pretend some process is running
app.processEvents()
app.exec_()
此代码只显示最后一张图片,而不是每张图片间隔 3 秒。关于如何解决这个问题的任何线索?
您必须创建指示状态和更改图像的信号:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Manager(QtCore.QObject):
"""Class that simulates the loading steps"""
started = QtCore.pyqtSignal()
loaded_modules = QtCore.pyqtSignal()
connected = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
def start(self):
self.started.emit()
for i, step in enumerate((self.loaded_modules, self.connect, self.finish), 1):
QtCore.QTimer.singleShot(i * 2000, step)
def load_modules(self):
self.loaded_modules.emit()
def connect(self):
self.connected.emit()
def finish(self):
self.finished.emit()
class SplashScreen(QtWidgets.QSplashScreen):
@QtCore.pyqtSlot()
def handle_started(self):
self.show()
self.setPixmap(QtGui.QPixmap("started.png"))
@QtCore.pyqtSlot()
def handle_loaded_modules(self):
self.setPixmap(QtGui.QPixmap("loaded_modules.png"))
@QtCore.pyqtSlot()
def handle_connected(self):
self.setPixmap(QtGui.QPixmap("connected.png"))
@QtCore.pyqtSlot()
def handle_finished(self):
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
w.resize(640, 480)
manager = Manager()
splash_screen = SplashScreen()
manager.started.connect(splash_screen.handle_started)
manager.loaded_modules.connect(splash_screen.handle_loaded_modules)
manager.connected.connect(splash_screen.handle_connected)
manager.finished.connect(splash_screen.handle_finished)
manager.finished.connect(w.show)
manager.start()
app.exec_()
if __name__ == "__main__":
main()