简体   繁体   English

从.qrc文件(使用pyside-rcc)编译的.py文件不起作用

[英]A .py file which compiled from .qrc file( using pyside-rcc ) does not work

I am working on python project and I have a problem with my .py file which complied from .qrc file. 我正在研究python项目,我的.py文件存在问题,该文件符合.qrc文件。 First, let I explain briefly about my project. 首先,让我简要介绍一下我的项目。

I created my project GUI in QtDesigner and also use the image in the GUI. 我在QtDesigner中创建了我的项目GUI,并且还使用了GUI中的图像。 Then, I generate .py from .ui file using pyside-uic and generate .py file from .qrc file using pyside-rcc. 然后,我使用pyside-uic从.ui文件生成.py,并使用pyside-rcc从.qrc文件生成.py文件。 The problem is when I use the .py file (an image file), images does not show in my GUI. 问题是当我使用.py文件(图像文件)时,图像不会显示在我的GUI中。

Is anybody knows how to solve this problem? 有谁知道如何解决这个问题?

Thank you for all your answer. 谢谢你的所有答案。 :) :)

Ps. PS。 I use PySide as my GUI language. 我使用PySide作为我的GUI语言。

Have you actually added the resource to your GUI project? 您是否真的将资源添加到GUI项目中?

Starting from scratch, here's how to do it: 从头开始,这是如何做到的:

In Qt Designer, select View > Resource Browser. 在Qt Designer中,选择“视图”>“资源浏览器”。 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 (eg 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 (eg "images") using the buttons below the right-hand pane, then add your images (or whatever), and finally click Ok. 创建资源文件后,使用右侧窗格下方的按钮添加前缀(例如“images”),然后添加图像(或其他),最后单击“确定”。

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. 现在,当您想要将pixmap添加到标签时,只需确保从新资源中选择图像,而不是磁盘上的文件。

The final steps are to re-generate your GUI module using pyside-uic and generate the resources module using pyside-rcc . 最后的步骤是使用pyside-uic重新生成GUI模块,并使用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: 执行此操作时,请确保资源模块在GUI模块所在的目录中保存为“resource_rc.py”:

    pyside-uic -o widget.py widget.ui
    pyside-rcc -o resource_rc.py resource.qrc

(PS: if your are using packages in your project, you should use the --from-imports option with pyside-uic to get relative imports). (PS:如果您在项目中使用包,则应使用带有pyside-uic --from-imports选项来获取相对导入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM