Python PYQT4 - 如何编译 qrc 文件并将其导入我的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15864762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PYQT4 - How do I compile and import a qrc file into my program?
提问by Thomas
I'm having trouble importing a resource file. I'm using pyqt4 with monkey studio and I am trying to import a png image. When I run the program I get an import error like
我在导入资源文件时遇到问题。我正在使用 pyqt4 和猴子工作室,我正在尝试导入一个 png 图像。当我运行程序时,我收到一个导入错误,如
ImportError: No module named icon_rc
导入错误:没有名为 icon_rc 的模块
I know that I have to compile it using pyrcc4 but I don't understand how to do this can anybody help please. It would be very helpful to have an answer that fully explains how to compile the resource file so I can import it.
我知道我必须使用 pyrcc4 编译它,但我不明白如何做到这一点,请任何人帮忙。有一个完整解释如何编译资源文件以便我可以导入它的答案将非常有帮助。
采纳答案by Thomas
Open cmd (or terminal on *nix) and run
打开 cmd(或 *nix 上的终端)并运行
pyrcc4 -py3 F:\computing\Payrollv22\icon.qrc -o icon_rc.py
It compiled the file successfully and I was able to import the py file into my project and run it with no problem.
它成功编译了文件,我能够将 py 文件导入我的项目并毫无问题地运行它。
回答by mata
There really isn't much to explain here, you have a resource file (e.g. icon.qrc), then you call pyrcc4 -o icon_rc.py icon.qrcwhich will create a module icon_rc.pywhich you then can import in your project.
这里真的没有什么要解释的,您有一个资源文件(例如icon.qrc),然后您调用pyrcc4 -o icon_rc.py icon.qrc它会创建一个模块,然后您可以将icon_rc.py其导入到您的项目中。
It's all documented here.
这一切都记录在这里。
回答by IFfy KhAn
In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py
在 Pyqt5 中可以使用此命令 Pyrcc5 input_file.qrc -o Out_file.py
We need to convert that qrc file into python file and then it can be imported to your code
我们需要将该 qrc 文件转换为 python 文件,然后才能将其导入到您的代码中
回答by Nilesh K.
you could try with pyside as well like:
您也可以尝试使用 pyside,例如:
--- pyside-rcc -o input.qrc output.py
--- pyside-rcc -o input.qrc output.py
回答by Storm Shadow
its because when you also used pyuic5 to convert your UI to py, the resource file name from the UI sticks.
这是因为当您还使用 pyuic5 将您的 UI 转换为 py 时,UI 中的资源文件名会保留下来。
then use
然后使用
Pyrcc5 input_file.qrc -o icons.py
remove from main_script.py
从 main_script.py 中删除
import icon_rc
and use
并使用
import icons
the when calling the actual icons from the icons module, you have to look at your qrc file prefix.
从图标模块调用实际图标时,您必须查看您的 qrc 文件前缀。
< RCC >
< qresource
prefix = "ico5" >
< file > plugin.png < / file >
< / qresource >
< / RCC >
if prefix is ico5 then you load icons with
如果前缀是 ico5 那么你加载图标
QtGui.QIcon(":/ico5/plugin.png")
and if prefix is , lets say,
如果前缀是,让我们说,
<RCC>
<qresource prefix="icons">
then its:
那么它的:
QtGui.QIcon(":/icons/plugin.png")

