温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

pyqt怎么添加启动等待界面的操作

发布时间:2021-03-17 14:11:40 来源:亿速云 阅读:797 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关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打包文件,打包为一个文件夹

补充:PyQt5启动画面——等待程序启动不用干瞪眼

PyQt的启动画面可以通过QSplashScreen类来快捷制作,支持透明图片

import sys
from PyQt5 import QtWidgets, QtGui
# 对Qt部件的操作一般都要在创建Qt程序后才能进行
app = QtWidgets.QApplication(sys.argv) 
# 创建启动界面,支持png透明图片
splash = QtWidgets.QSplashScreen(QtGui.QPixmap('splash.png'))
splash.show()
# 可以显示启动信息
splash.showMessage('正在加载……')
# 关闭启动画面
splash.close()

感谢各位的阅读!关于“pyqt怎么添加启动等待界面的操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI