pyqt添加啟動等待界面


一、實驗環境

1.Windows7x64_SP1

2.anaconda3.7 + python3.7(anaconda集成,不需單獨安裝)

3.pyinstaller3.5        #使用pyinstaller3.0,打包時報錯

 

二、實驗步驟

1.resource.qrc文件中添加圖片資源

<RCC>
  <qresource>
    <file alias="contacts.png">ico/contacts.png</file>
    <file alias="exit.png">ico/exit.png</file>
    <file alias="about.png">ico/about.png</file>
    <file alias="config.png">ico/config.png</file>
    <file alias="help.png">ico/help.png</file>
    <file alias="xel.png">ico/xel.png</file>
    <file alias="xel_small.png">ico/xel_small.png</file>
    <file alias="magnifier.png">ico/magnifier.png</file>
    <file alias="wait.png">ico/wait.png</file>
  </qresource>
</RCC>

2.使用pyrcc5生成最新的resource.py文件,命令參考如下:

pyrcc5 -o resource.py resource.qrc

3.更改程序主文件

# -*- coding: utf-8 -*-
import os,sys,time,re
from PyQt5.QtWidgets import QMainWindow,QMessageBox
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QApplication,QDialog,QSplashScreen
from PyQt5 import QtCore,QtGui
from untitled import Ui_MainWindow


if __name__ == '__main__':
    app = QApplication(sys.argv)
    splash = QSplashScreen(QtGui.QPixmap(":/wait.png"))     # 啟動界面圖片地址
    splash.show()                                           # 展示啟動圖片
    app.processEvents()                                     # 防止進程卡死
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/magnifier.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    tool = SmartBit_Tool()
    tool.setWindowIcon(icon)
    tool.show()
    splash.finish(tool)                                     # 關閉啟動界面
    sys.exit(app.exec_())

4.運行結果

運行exe文件發現,從雙擊exe文件至彈出啟動界面圖片,還是存在一定的延時!如何縮短這個延時,還需要繼續研究,知道的童鞋也請告知!

5.提升運行速度

    • 代碼中不用的庫,刪除其導入語句
    • 盡量使用from xxx import xxx,少用from xxx import *
    • 使用pyinstaller打包文件,打包為一個文件夾


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM