1. 程式人生 > >PyQt5開發小記,如何實現程序啟動畫面和退出提示?

PyQt5開發小記,如何實現程序啟動畫面和退出提示?

argv qpi spl fin 對象 測試數據 pla widgets 分享

程序啟動畫面:
if __name__ == "__main__":
    ui_style = UIStyle().get_style()
    app = QtWidgets.QApplication(sys.argv)
        # 創建QSplashScreen對象實例
    splash = QtWidgets.QSplashScreen(QtGui.QPixmap("UserView/logo.png"))
        # 設置畫面中的文字的字體
    splash.setFont(QFont(‘Microsoft YaHei UI‘, 12))
        # 顯示畫面
    splash.show()
        # 顯示信息
    splash.showMessage("啟動中... 0%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    time.sleep(1)
    splash.showMessage("正在加載樣式表...20%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    if(ui_style==‘dark‘):
        app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    time.sleep(1)
    splash.showMessage("正在加載樣式表...40%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    time.sleep(1)
    splash.showMessage("正在加載數據庫配置...60%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    time.sleep(1)
    splash.showMessage("正在測試數據庫連接...80%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    conn = GetMysqlConnection().get_connection()
    if(conn!=0):
        conn.close()
        splash.showMessage("正在測試數據庫連接...Success", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    else:
        splash.showMessage("正在測試數據庫連接...faild", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    time.sleep(2)
    splash.showMessage("啟動中...100%", QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom, QtCore.Qt.white)
    time.sleep(2)
    ui = MainWindow()
    ui.show()
        # 當主界面顯示後銷毀啟動畫面
    splash.finish(ui)
    sys.exit(app.exec_())

技術分享圖片

程序退出提示

    # 實現這個函數
        def closeEvent(self, QCloseEvent):
        #  使用QMessageBox提示
        reply = QMessageBox.warning(self, "溫馨提示", "即將退出, 確定?", QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
        if(reply == QMessageBox.Yes):
            QCloseEvent.accept()
        if(reply==QMessageBox.No):
            QCloseEvent.ignore()

技術分享圖片

PyQt5開發小記,如何實現程序啟動畫面和退出提示?