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

python基础

浏览数:117

2019-9-14

AD:资源代下载服务

程序启动画面:

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

作者:BlueMiaomiao