【问题标题】:Create Splash screen in PyQt5在 PyQt5 中创建启动画面
【发布时间】:2020-02-27 21:17:37
【问题描述】:

我想使用 PythonPyQt5 中创建启动画面。我搜索了,但我在 Pyqt4 中找到了,我对 PyQt4 不了解,所以在这种情况下帮助我,我将不胜感激

Splash screen in pyqt

【问题讨论】:

    标签: python user-interface pyqt5 splash-screen


    【解决方案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_())
    


    示例 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_())
    

    【讨论】:

    • 感谢您的回答,但是当我单击按钮时出现混乱,它仅显示灰屏;不是图像.. 背后的原因是什么??
    • @sabeenkanwal 您必须指定您的图像路径self.splash = QSplashScreen(QPixmap('path/name.jpg'))。如果您的图像位于当前目录(您启动应用程序的位置),您可以简单地指定其名称self.splash = QSplashScreen(QPixmap('name.jpg'))
    • 我们可以在启动画面中只显示图像吗?
    • @sabeenkanwal 我添加了第二个示例
    【解决方案2】:

    我喜欢在加载我的主小部件之前添加它并稍微淡化 - 请注意,这仅对显示徽标有用,如果您的应用程序加载时间很长,您可以使用像 @S. Nick 这样的启动屏幕如上所示,在您显示启动画面时允许加载时间:

    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.06
        splash.setWindowOpacity(opaqueness)
        splash.show()
        while opaqueness < 1:
            splash.setWindowOpacity(opaqueness)
            time.sleep(step) # Gradually appears
            opaqueness+=step
        time.sleep(1.2)
        splash.close()
    
        # Run the normal application
        widget.show()
        app.exec_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多