python 在PyQt5中创建启动画面

vqlkdk9b  于 2022-12-17  发布在  Python
关注(0)|答案(3)|浏览(307)

我想使用Python在**PyQt5中创建一个启动画面。我进行了搜索,但在Pyqt4中找到,而且我不了解PyQt4**,因此在这种情况下帮助我将是无偿的
Splash screen in pyqt

ipakzgxi

ipakzgxi1#

我喜欢在加载我的主小部件之前添加它,稍微淡入淡出-注意,这只对显示徽标有用,如果您的应用程序加载时间很长,您可以使用@S. Nick所示的闪屏,以便在显示闪屏时允许加载时间:

from PyQt5 import QtGui, QtCore, QtWidgets
import time

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    # Create splashscreen
    splash_pix = QtGui.QPixmap('picture.png')
    splash = QtWidgets.QSplashScreen(splash_pix, QtCore.Qt.WindowStaysOnTopHint)
    # add fade to splashscreen 
    opaqueness = 0.0
    step = 0.1
    splash.setWindowOpacity(opaqueness)
    splash.show()
    while opaqueness < 1:
        splash.setWindowOpacity(opaqueness)
        time.sleep(step) # Gradually appears
        opaqueness+=step
    time.sleep(1) # hold image on screen for a while
    splash.close() # close the splash screen
    #widget = YourWidget()
    #widget.show() # This is where you'd run the normal application
    app.exec_()
41ik7eoe

41ik7eoe2#

试试看:

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

owfi6suc

owfi6suc3#

我发现的最简单最好的例子。
https://www.youtube.com/watch?v=TsatZJfzb_Q&t=162s

from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog
from PyQt5.QtWidgets import QGraphicsScene,QSplashScreen
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

class SplashScreen(QSplashScreen):
    def __init__(self):
        super(QSplashScreen, self).__init__()
        loadUi("splash.ui", self)
        self.setWindowFlag(Qt.FramelessWindowHint)
        pixmap = QPixmap("any_image.jpg")
        self.setPixmap(pixmap)

    def progress(self):
        for i in range(40):
            time.sleep(0.1)
            self.progressBar.setValue(i)

class MainScreen(QMainWindow):
    def function1():
       .......
    def function2():
       ......
    
if __name__ == "__main__":
    app = QApplication(sys.argv)

    splash = SplashScreen()
    splash.show()
    splash.progress()

    mainscreen = MainScreen()
    mainscreen.show()

    splash.finish(widget)

    sys.exit(app.exec_())

相关问题