PYQT4 -如何编译和导入一个qrc文件到我的程序中?

时间:2021-06-16 07:07:06

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和monkey studio,我正在尝试导入一个png图像。当我运行程序时,我得到了一个导入错误。

ImportError: No module named icon_rc

ImportError:没有名为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编译它,但是我不知道怎么做,任何人都可以帮忙。有一个完整地解释如何编译资源文件以便导入它的答案将非常有帮助。

5 个解决方案

#1


26  

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文件导入到我的项目中,并且没有任何问题。

#2


19  

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.qrc which will create a module icon_rc.py which you then can import in your project.

这里没有太多需要解释的东西,您有一个资源文件(例如icon.qrc),然后调用pyrcc4 -o icon_rc。py图标。将创建一个模块icon_rc。然后你可以在项目中导入py。

It's all documented here.

这里的所有记录。

#3


2  

In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py

在Pyqt5中,这个命令可以使用Pyrcc5 input_file。独有的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文件,然后它可以导入到您的代码中。

#4


0  

you could try with pyside as well like:

你可以试试pyside:

--- pyside-rcc -o input.qrc output.py

- - - pyside-rcc - o输入。独有的output.py

#5


0  

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")

#1


26  

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文件导入到我的项目中,并且没有任何问题。

#2


19  

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.qrc which will create a module icon_rc.py which you then can import in your project.

这里没有太多需要解释的东西,您有一个资源文件(例如icon.qrc),然后调用pyrcc4 -o icon_rc。py图标。将创建一个模块icon_rc。然后你可以在项目中导入py。

It's all documented here.

这里的所有记录。

#3


2  

In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py

在Pyqt5中,这个命令可以使用Pyrcc5 input_file。独有的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文件,然后它可以导入到您的代码中。

#4


0  

you could try with pyside as well like:

你可以试试pyside:

--- pyside-rcc -o input.qrc output.py

- - - pyside-rcc - o输入。独有的output.py

#5


0  

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")