Pyqt 打包资源文件
用打包工具将做好的Pyqt程序打包成exe后发现引用的资源图片都显示不了?
是否遇到了和我一样的问题呢。google之后找到了方法,一种方法是在程序中引用外部资源,另外一种方法是将资源文件转换为py文件,然后引用py。下面我们详细来实现后者方法实现的过程
一. 生成资源文件
要把资源文件转换为py文件首先要有资源文件,我们打开Qt Designer,添加资源文件
保存好资源文件后缀是以 .qrc 结尾的,用notepad++查看.qrc文件发现该文件其实为XML
- <RCC>
- <qresource prefix="picture">
- <file>img/270r.png</file>
- <file>img/440kb.png</file>
- <file>img/firefox.png</file>
- <file>img/notepad.png</file>
- <file>img/python.png</file>
- <file>img/cut.png</file>
- <file>img/python.jpg</file>
- <file>img/loading.gif</file>
- </qresource>
- </RCC>
二. 创建一个窗体
用Qt Designer 创建一个最简单的窗体
然后将.ui文件转换成 py文件
- # -*- coding: utf-8 -*-
- # Form implementation generated from reading ui file 'resource.ui'
- #
- # Created: Tue Jan 20 17:17:34 2015
- # by: PyQt4 UI code generator 4.10.3
- #
- # WARNING! All changes made in this file will be lost!
- from PyQt4 import QtCore, QtGui
- try:
- _fromUtf8 = QtCore.QString.fromUtf8
- except AttributeError:
- def _fromUtf8(s):
- return s
- try:
- _encoding = QtGui.QApplication.UnicodeUTF8
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig, _encoding)
- except AttributeError:
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig)
- class Ui_resource(object):
- def setupUi(self, resource):
- resource.setObjectName(_fromUtf8("resource"))
- resource.resize(400, 300)
- self.retranslateUi(resource)
- QtCore.QMetaObject.connectSlotsByName(resource)
- def retranslateUi(self, resource):
- resource.setWindowTitle(_translate("resource", "Form", None))
三. 转换资源文件
转换资源文件用的是Pyqt的pyrcc4 命令
- pyrcc4 qrcfile.qrc -o pyfile.py
1.Pycharm集成pyrcc4
我们使用Pycharm来集成pyrcc4,这样更利于我们高效开发
首先在菜单里面找到 File => settings => Tools => External Tools (外边工具设置)
选择添加Add
Name 填写: Rcc2Py
Group: 自已任意填写,我填写的是PyQt4
下面的Options默认
在Tools settings 里面这样填写:
Program 就是你安装Pyqt4的路径
Parameters 是指转换的参数 $FileName$ -o $FileNameWithoutExtension$.py
Working directory 表示输出在当前的工作目录 $FileDir$
2.转换qrc为py
选择要转换的qrc文件,右键,找到group 为(PyQt4) 目录下的Rcc2Py
转换完成后,同级目录下就多出一个与qrc文件同命名的py文件,查看文件:
- # -*- coding: utf-8 -*-
- # Resource object code
- #
- # Created: 周二 一月 20 11:43:03 2015
- # by: The Resource Compiler for PyQt (Qt v4.8.5)
- #
- # WARNING! All changes made in this file will be lost!
- from PyQt4 import QtCore
- qt_resource_data = "\
- \x00\x00\x05\x2b\
- \x89\
- \x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
- \x00\x00\x20\x00\x00\x00\x20\x08\x06\x00\x00\x00\x73\x7a\x7a\xf4\
- \x00\x00\x00\x04\x67\x41\x4d\x41\x00\x00\xd6\xd8\xd4\x4f\x58\x32\
- \x00\x00\x00\x19\x74\x45\x58\x74\x53\x6f\x66\x74\x77\x61\x72\x65\
- \x00\x41\x64\x6f\x62\x65\x20\x49\x6d\x61\x67\x65\x52\x65\x61\x64\
- \x79\x71\xc9\x65\x3c\x00\x00\x04\xbd\x49\x44\x41\x54\x58\xc3\xed\
- \x57\x6b\x4c\x93\x57\x18\x3e\x23\x71\xc9\x32\xe9\x16\x97\xa8\x54\
- \x65\x38\x9d\x02\x15\xf6\x03\x87\x32\x93\x01\x66\x2c\x5b\x70\xc4\
- \x30\xff\x60\xa2\x2e\x1a\x3a\x1d\x4e\x03\xba\x31\x89\x5b\xb3\x80\
- \xd9\x0c\x84\x02\x19\x58\x1c\x14\x8b\x85\xb2\x82\x95\x5e\xe4\x66\
- \x0b\x8e\x31\xf8\xc3\x46\xcb\x2d\x81\x15\xdc\xa8\xc2\x1c\x1b\xb7\
- \x6a\x69\x91\xf2\xee\xbc\x87\xaf\x0c\xdc\xb8\x0d\x61\xd9\xb2\x93\
- \x3c\xed\x97\xf3\x7d\xfd\xde\xe7\xbc\xef\xf3\x5e\x4a\x00\x80\xfc\
- \x93\x20\xff\x0a\x02\x74\x09\x28\x44\x14\xd9\x14\x71\x14\x01\x2b\
- \x46\x80\xae\xdd\x64\xdd\xc6\x66\x22\x4c\xf8\x95\xc4\x8b\x47\xc8\
- \xa1\xd3\xf7\xc8\x8e\x97\x3b\x38\x32\x61\x2b\x41\x20\x85\x9c\xbe\
- \x30\x48\x2e\xdd\x80\x19\x40\x32\xab\x79\x4d\xf4\xbe\xfb\x72\x13\
- \x68\x64\x06\x91\x04\x5e\xa3\x51\xf4\x06\xee\x85\x47\xf5\xd0\xbd\
- \x83\xcb\x4d\x20\x9b\x9d\xf6\x40\x74\x2f\xbd\x16\x32\x3d\x20\x89\
- \x3f\x48\xa5\x2c\x1b\x01\x8c\x31\x79\xc1\xbb\x9d\x88\x4b\xc6\xd7\
- \xc6\x26\x0e\xa0\x10\xb9\xfd\x42\xfe\xc5\x2b\x36\x46\x8c\x12\x5c\
- \x4e\x02\x93\xa7\xa7\xa7\x0d\xcc\xd3\x39\xb9\x98\x63\x36\x14\x0a\
- …………………………
- ………………………… # 此处省略1000行
- …………………………
- qt_resource_struct = "\
- \x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
- \x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
- \x00\x00\x00\x14\x00\x02\x00\x00\x00\x08\x00\x00\x00\x03\
- \x00\x00\x00\x4e\x00\x01\x00\x00\x00\x01\x00\x00\x3a\x1b\
- \x00\x00\x00\xce\x00\x00\x00\x00\x00\x01\x00\x09\x24\x4c\
- \x00\x00\x00\x9c\x00\x00\x00\x00\x00\x01\x00\x01\x90\xfe\
- \x00\x00\x00\x86\x00\x00\x00\x00\x00\x01\x00\x00\xa3\x09\
- \x00\x00\x00\x20\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
- \x00\x00\x00\x6a\x00\x00\x00\x00\x00\x01\x00\x00\x79\x75\
- \x00\x00\x00\x34\x00\x00\x00\x00\x00\x01\x00\x00\x05\x2f\
- \x00\x00\x00\xb4\x00\x00\x00\x00\x00\x01\x00\x08\x72\xdb\
- "
- def qInitResources():
- QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)
- def qCleanupResources():
- QtCore.qUnregisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)
- qInitResources()
该文件已经使用QtCore.qRegisterResourceData 对文件初始化注册,所以我们可以直接引用该文件
四. 引用资源py文件
py文件生成好了如何来引用使用呢?
说对了,引用就这么简单
- import apprcc
使用的时候 冒号 “ : ” 加 图片的路径, 如:
- :/img/firefox.png
运行试试,发现图片不显示,为什么呢,因为qrc文件添加过程中,我加了一个 “前缀” prefix。 所以,如果在qrc文件中不添加前缀 使用 :/img/firefox.png 是可以的,但添加了前缀生成的qrc文件 qresource标签会多一个属性
- qresource prefix="picture"
在这里,正确的使用是:
- :picture/img/firefox.png
完整的代码如下:
- # -*- coding: utf-8 -*-
- # Form implementation generated from reading ui file 'resource.ui'
- #
- # Created: Tue Jan 20 17:17:34 2015
- # by: PyQt4 UI code generator 4.10.3
- #
- # WARNING! All changes made in this file will be lost!
- from PyQt4 import QtCore, QtGui
- try:
- _fromUtf8 = QtCore.QString.fromUtf8
- except AttributeError:
- def _fromUtf8(s):
- return s
- try:
- _encoding = QtGui.QApplication.UnicodeUTF8
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig, _encoding)
- except AttributeError:
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig)
- class Ui_resource(object):
- def setupUi(self, resource):
- resource.setObjectName(_fromUtf8("resource"))
- resource.resize(400, 300)
- self.retranslateUi(resource)
- QtCore.QMetaObject.connectSlotsByName(resource)
- def retranslateUi(self, resource):
- resource.setWindowTitle(_translate("resource", "Form", None))
- import apprcc
- class showresource(QtGui.QWidget):
- def __init__(self):
- super(showresource, self).__init__()
- self.ui_resource = Ui_resource()
- self.ui_resource.setupUi(self)
- self.setWindowTitle('ResourceToPy')
- palette1 = QtGui.QPalette(self)
- palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap(':picture/img/firefox.png')))
- self.setPalette(palette1)
- # 重载keyPressEvent , 当按下Esc退出
- def keyPressEvent(self, event):
- if event.key() ==QtCore.Qt.Key_Escape:
- self.close()
- if __name__ == "__main__":
- import sys
- app = QtGui.QApplication(sys.argv)
- resource = showresource()
- resource.show()
- sys.exit(app.exec_())
五. 打包运行
我使用的是Pyinstaller打包成单一的exe,运行exe查看效果
Pyqt 打包资源文件的更多相关文章
- VS打包资源文件,转自推酷,请小星同学查看
上篇博客把收费系统的总体设计进行了一遍讲解,讲解的同时掺杂了些有关.NET编译机制的总结.程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的是没有了打包工具.不知道出于什么原因 ...
- Webpack 使用url-loader和file-loader打包资源文件
在js中不仅可以通过import引入js文件,还可以引入图片.视频等资源文件,这样webpack打包时就会把所引入的资源文件也一起打包进来 打包进来的文件会返回一个字符串:即文件的路径 要做到这一点, ...
- maven打包资源文件(转)
原文链接:http://blog.csdn.net/u012849872/article/details/51035938 maven工程标准目录结构: src -main –bin ...
- pyinstall打包资源文件
相关代码 main.py import sys import os #生成资源文件目录访问路径 #说明: pyinstaller工具打包的可执行文件,运行时sys.frozen会被设置成True # ...
- ******可用 SpringBoot 项目打包分开lib,配置和资源文件
spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...
- spring/boot 打包,资源/配置/业务文件分离
spring/boot打包,将业务jar包和资源配置文件进行分离打包,打包后的资源在target/release文件夹下面 注意:添加以下配置后,注意修改自己的入口类 <!--相关编译打包依赖- ...
- 使用webpack打包ThinkPHP的资源文件
使用webpack打包ThinkPHP的资源文件 利用自己的空余时间一直在维护http://www.wx2share.com这个小网站,全是一个人在弄,由于只租得起虚拟空间,所以后台采用了简单方便的T ...
- 打包jar文件 外部调用资源 so等
一个非常好的从jar文件中加载so动态库方法,在android的gif支持开源中用到.这个项目的gif解码是用jni c实现的,避免了OOM等问题. 项目地址:https://github.com/k ...
- Android将Activity 打 jar包 (解决资源文件不能打包的问题)
转载地址:http://blog.csdn.net/xiaanming/article/details/9257853 最近有一个需要,我们公司做了一个apk客户端,然后其他的公司可以根据自己的需要来 ...
随机推荐
- UDS帧传输
说明 在UDS协议中,其中有一点我视作为基础,即帧传输.也即是数据传输这一块,在UDS的帧传输中,分为4种: SF单帧 FF第一帧 CF连续帧 FC流控制帧 首先,我们抛开以上的东西,假设一个销售商( ...
- Intellij IDEA使用总结
查询快捷键CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查 找类中的方法或变量CIRL+B 找变量的来源CTRL+ALT+B 找所有的子类CTRL ...
- django-cms安装
ubuntu:12.04 (32bit) djangocms 0.5.1 =========================== 首先,跟着这个做: https://github.com/divio/ ...
- MyEclipse 10 集成Maven
第一步:安装Maven,作者安装目录是:D:\Java\apache-maven-3.2.5 第二步:配置本地仓库 maven将每次应用过的项目.文件.jar都会存储到maven的仓库中(默认仓库位置 ...
- 不懂点CAP理论,你好意思说你是做分布式的吗?
- poj 1035
http://poj.org/problem?id=1035 poj的一道字符串的水题,不难,但就是细节问题我也wa了几次 题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出 ...
- android通过Canvas和Paint截取无锯齿圆形图片
一个通过Canvas和Paint截取无锯齿圆形图片. /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public st ...
- FastReport经验
FastReport经验 1.FastReport中如果访问报表中的对象? 可以使用FindObject方法. TfrxMemoView(frxReport1.FindObject(’memo1′)) ...
- ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)
第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...