使用 GIF 和图像创建启动画面
Create splash screen with GIF and image
我想要一个包含图像和 GIF 的启动画面(gif 将位于图像之上)。
我已经这样做了,但我认为这不是一个好方法,并且由于我使用了计时器,所以我放弃了使用启动画面的想法。
如何避免使用计时器。
这是我的最后一个代码:
import time
from PyQt5.QtCore import QRect, QMetaObject, QCoreApplication, Qt
from PyQt5.QtGui import QMovie, QPixmap
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QApplication, QWidget
class Ui_Form(QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
self.setObjectName("self")
self.resize(400, 400)
self.label = QLabel(self)
self.label.setText("This is main application")
self.label.setObjectName("label")
def showStartScreen():
start = time.time()
# PNG image
img_path = f".../splash_image.PNG"
image = QPixmap(img_path)
# set layout in order to put GIF in above (on top) a word that are in splash image
layout = QHBoxLayout()
# big image for Splash screen
image_container = QLabel()
image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
image_container.setLayout(layout)
image_container.setPixmap(image)
# label for displaying GIF
label_2 = QLabel()
label_2.setStyleSheet("margin-bottom:21px; margin-right:21px")
movie = QMovie(".../loading_text_border_animation.gif")
label_2.setMovie(movie)
layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
movie.start()
image_container.show()
# My problem is here, As you can say i made a timer for 5s in order to keep that GIF moving,
# so making splash screen here is pointless, because
# for example if the app needs only 3s to start then we slow it down by 2s
while time.time() < start + 5:
app.processEvents()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Ui_Form()
showStartScreen()
ui.show()
sys.exit(app.exec_())
一种可能的解决方案是使用 QTimer:
image_container = QLabel()
image_container.setAttribute(Qt.WA_DeleteOnClose) // <--
image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
image_container.setLayout(layout)
image_container.setPixmap(image)
# ...
layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
movie.start()
image_container.show()
return image_container
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Ui_Form()
splash_screen = showStartScreen()
QTimer.singleShot(5 * 1000, splash_screen.close)
QTimer.singleShot(5000, ui.show)
sys.exit(app.exec_())
我想要一个包含图像和 GIF 的启动画面(gif 将位于图像之上)。
我已经这样做了,但我认为这不是一个好方法,并且由于我使用了计时器,所以我放弃了使用启动画面的想法。
如何避免使用计时器。
这是我的最后一个代码:
import time
from PyQt5.QtCore import QRect, QMetaObject, QCoreApplication, Qt
from PyQt5.QtGui import QMovie, QPixmap
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QApplication, QWidget
class Ui_Form(QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
self.setObjectName("self")
self.resize(400, 400)
self.label = QLabel(self)
self.label.setText("This is main application")
self.label.setObjectName("label")
def showStartScreen():
start = time.time()
# PNG image
img_path = f".../splash_image.PNG"
image = QPixmap(img_path)
# set layout in order to put GIF in above (on top) a word that are in splash image
layout = QHBoxLayout()
# big image for Splash screen
image_container = QLabel()
image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
image_container.setLayout(layout)
image_container.setPixmap(image)
# label for displaying GIF
label_2 = QLabel()
label_2.setStyleSheet("margin-bottom:21px; margin-right:21px")
movie = QMovie(".../loading_text_border_animation.gif")
label_2.setMovie(movie)
layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
movie.start()
image_container.show()
# My problem is here, As you can say i made a timer for 5s in order to keep that GIF moving,
# so making splash screen here is pointless, because
# for example if the app needs only 3s to start then we slow it down by 2s
while time.time() < start + 5:
app.processEvents()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Ui_Form()
showStartScreen()
ui.show()
sys.exit(app.exec_())
一种可能的解决方案是使用 QTimer:
image_container = QLabel()
image_container.setAttribute(Qt.WA_DeleteOnClose) // <--
image_container.setWindowFlag(Qt.SplashScreen, Qt.FramelessWindowHint)
image_container.setLayout(layout)
image_container.setPixmap(image)
# ...
layout.addWidget(label_2, 0, Qt.AlignRight | Qt.AlignBottom)
movie.start()
image_container.show()
return image_container
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = Ui_Form()
splash_screen = showStartScreen()
QTimer.singleShot(5 * 1000, splash_screen.close)
QTimer.singleShot(5000, ui.show)
sys.exit(app.exec_())