在PyQt5中创建启动画面

时间:2019-11-01 15:11:08

标签: python user-interface pyqt5 splash-screen

我想使用 Python PyQt5 中创建启动画面。我搜索了,但是在 Pyqt4 中找到了,并且我对 PyQt4 不了解,所以在这种情况下请帮助我

Splash screen in pyqt

2 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout, QApplication, QSplashScreen 
from PyQt5.QtCore import QTimer

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

        self.b1 = QPushButton('Display screensaver')
        self.b1.clicked.connect(self.flashSplash)

        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.b1)

    def flashSplash(self):
        self.splash = QSplashScreen(QPixmap('D:/_Qt/img/pyqt.jpg'))

        # By default, SplashScreen will be in the center of the screen.
        # You can move it to a specific location if you want:
        # self.splash.move(10,10)

        self.splash.show()

        # Close SplashScreen after 2 seconds (2000 ms)
        QTimer.singleShot(2000, self.splash.close)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Dialog()
    main.show()
    sys.exit(app.exec_())

enter image description here


示例2

import sys
from PyQt5 import QtCore, QtGui, QtWidgets     # + QtWidgets


import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore    import QTimer, Qt

if __name__ == '__main__':
    app = QApplication(sys.argv)

    label = QLabel("""
            <font color=red size=128>
               <b>Hello PyQt, The window will disappear after 5 seconds!</b>
            </font>""")

    # SplashScreen - Indicates that the window is a splash screen. This is the default type for .QSplashScreen
    # FramelessWindowHint - Creates a borderless window. The user cannot move or resize the borderless window through the window system.
    label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
    label.show()

    # Automatically exit after  5 seconds
    QTimer.singleShot(5000, app.quit) 
    sys.exit(app.exec_())

enter image description here

答案 1 :(得分:0)

我喜欢在我加载主小部件之前添加它,并带有轻微的淡入淡出 - 请注意,这仅对显示徽标有用,如果您的应用程序加载时间很长,您可以使用启动画面,如 {{3}已在上面显示以允许在显示启动画面时加载时间:

if __name__ == "__main__":
    app = QApplication([])
    # Create splashscreen
    splash_pix = QtGui.QPixmap('icons/Image.png')
    splash = QtGui.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
    widget = Main()
    # add fade to splashscreen 
    opaqueness = 0.0
    step = 0.03
    splash.setWindowOpacity(opaqueness)
    splash.show()
    while opaqueness < 1:
        splash.setWindowOpacity(opaqueness)
        time.sleep(step) # Sleep for 3 seconds
        opaqueness+=(2*step)
    time.sleep(1.2)
    splash.close()

    # Run the normal application
    widget.show()
    app.exec_()