pyqt Add Action to Start Waiting Interface

  • 2021-10-11 19:07:41
  • OfStack

1. Experimental environment

1.Windows7x64_SP1

2. anaconda3.7 + python3.7 (anaconda integration, no separate installation required)

3. pyinstaller3.5 # Use pyinstaller3.0, pack and report errors

2. Experimental steps

1. Add a picture resource to the resource. qrc file


<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. Use pyrcc5 to generate the latest resource. py file with the following command reference:


pyrcc5 -o resource.py resource.qrc

3. Change the program master file


# -*- 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"))   #  Startup interface picture address 
  splash.show()                      #  Show the startup picture 
  app.processEvents()                   #  Prevent the process from jamming 
  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)                   #  Close the startup interface 
  sys.exit(app.exec_())

4. Running results

Running exe file found that from double-clicking exe file to pop-up startup interface picture, there is still a delay of 1! How to shorten this delay, still need to continue to study, know children's shoes also please let us know!

STEP 5 Speed up

Delete the import statement for libraries that are not used in the code

Try to use from xxx import xxx and use less from xxx import *

Package files into 1 folder using pyinstaller

Supplement: PyQt5 startup screen-wait for the program to start without staring

The startup screen of PyQt can be made quickly through QSplashScreen class, and transparent pictures are supported


import sys
from PyQt5 import QtWidgets, QtGui
#  Right Qt Operation of components 1 Generally, it is necessary to create Qt Procedures before proceeding 
app = QtWidgets.QApplication(sys.argv) 
#  Create a startup interface to support png Transparent picture 
splash = QtWidgets.QSplashScreen(QtGui.QPixmap('splash.png'))
splash.show()
#  You can display startup information 
splash.showMessage(' Loading … ')
#  Close the startup screen 
splash.close() 

Related articles: