问题描述
我正在处理python项目,我的.py文件中存在问题,该文件符合.qrc文件。首先,让我简要介绍一下我的项目。
I am working on python project and I have a problem with my .py file which complied from .qrc file. First, let I explain briefly about my project.
我在QtDesigner中创建了项目GUI,并使用了GUI中的图像。然后,我使用pyside-uic从.ui文件生成.py,并使用pyside-rcc从.qrc文件生成.py文件。问题是当我使用.py文件(图像文件)时,图像不会显示在我的GUI中。
I created my project GUI in QtDesigner and also use the image in the GUI. Then, I generate .py from .ui file using pyside-uic and generate .py file from .qrc file using pyside-rcc. The problem is when I use the .py file (an image file), images does not show in my GUI.
有人知道如何解决这个问题吗?
Is anybody knows how to solve this problem?
感谢您的所有答案。 :)
Thank you for all your answer. :)
Ps。我使用PySide作为我的GUI语言。
Ps. I use PySide as my GUI language.
推荐答案
您是否真的将资源添加到GUI项目中?
Have you actually added the resource to your GUI project?
从头开始,这是如何做到的:
Starting from scratch, here's how to do it:
在Qt Designer中,选择View> Resource Browser。然后,在资源浏览器中,单击编辑资源按钮。从那里,您可以创建新的资源文件,也可以打开现有文件。重要的是资源文件必须能够使用相对路径访问资源(例如图像文件)。这意味着它们必须与资源文件或其子目录位于同一目录中。
In Qt Designer, select View > Resource Browser. Then, in the Resource Browser, click the Edit Resources button. From there, you can either create a new resouce file, or open an existing one. The important thing is that the resource file must be able to access the resources (e.g. image files) using relative paths. So that means they must be either in the same directory as the resource file, or one of its sub-directories.
创建资源文件后,添加一个使用右侧窗格下方的按钮添加前缀(例如图像),然后添加图像(或其他),最后单击确定。
Once you've created the resource file, add a prefix (e.g. "images") using the buttons below the right-hand pane, then add your images (or whatever), and finally click Ok.
现在需要时要将pixmap添加到标签,只需确保从新资源中选择图像,而不是磁盘上的文件。
Now when you want to add a pixmap to a label, just make sure you select the image from your new resource, rather than the file on disk.
最后的步骤是重新生成您的GUI模块使用 pyside-uic
并使用 pyside-rcc
生成资源模块。执行此操作时,请确保资源模块在GUI模块的相同目录中保存为resource_rc.py:
The final steps are to re-generate your GUI module using pyside-uic
and generate the resources module using pyside-rcc
. When you do this, make sure the resources module is saved as "resource_rc.py" in the same directory as the GUI module:
pyside-uic -o widget.py widget.ui
pyside-rcc -o resource_rc.py resource.qrc
(PS:如果您在项目中使用包,则应使用 - from-imports
选项与 pyside-uic
获得相对进口商品。)
(PS: if your are using packages in your project, you should use the --from-imports
option with pyside-uic
to get relative imports).
这篇关于从.qrc文件(使用pyside-rcc)编译的.py文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!