千家信息网

PyQt5开发小记,如何实现程序启动画面和退出提示?

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,程序启动画面:if __name__ == "__main__": ui_style = UIStyle().get_style() app = QtWidgets.QApplicatio
千家信息网最后更新 2025年01月23日PyQt5开发小记,如何实现程序启动画面和退出提示?

程序启动画面:

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

0