本文介绍了将资源文件导入 PyQt 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过Qt的纪录片和很多与这个不太相似的问题,但我仍然不知道我该怎么做.

I have seen Qt documentary and a lot of questions less-similar to this one, But i still haven't figured out how can i do it.

我不完全确定如何将资源文件导入 Python 代码,因此 pixmap 出现时没有任何问题.

I'm not entirely sure how can i import resource file to Python code, so pixmap appears without any issues.

我把所有文件都放在同一个目录下,我创建了 qrc.文件并使用以下命令编译它:rcc -binary resources.qrc -o res.rcc 以制作资源文件.

I have all files in same directory, I created qrc. file and compiled it with: rcc -binary resources.qrc -o res.rcc to make resource file.

我导入了 res_rcc 但标签上的像素图仍未显示:

I imported res_rcc but pixmap on label was still not shown:

导入 res_rcc

这就是我的 qrc 中的内容.文件:

This is what i had in my qrc. file:

<RCC>
  <qresource prefix="newPrefix">
    <file>download.jpeg</file>
  </qresource>
</RCC>

问题:

如何在 PyQt 代码中导入资源文件?| 如果像素图和.qrc 资源文件在同一个目录下,我还需要指定完整路径吗?

Question:

How can i import resource files in the PyQt code ? | If pixmaps are in same directory as .qrc resource files, Do i still need to specify full path?

推荐答案

对于 pyqt,你必须使用 pyrcc4,它相当于 Python 的 rcc.

For pyqt you have to use pyrcc4, which is the equivalent of rcc for python.

pyrcc4 -o resources.py resources.qrc

这会生成需要在 python 代码中导入的 resources.py 模块,以使资源可用.

This generates the resources.py module that needs to be imported in the python code in order to make the resources available.

import resources

要在代码中使用资源,您必须使用:/";前缀:

To use the resource in your code you have to use the ":/" prefix:

示例

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import resources

pixmap = QPixmap(":/newPrefix/download.jpeg")

请参阅PyQt4 资源系统Qt 资源系统

这篇关于将资源文件导入 PyQt 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:08