简体   繁体   English

在python程序中包括图像?

[英]Including images in python program?

I have what is basically a custom version of PySide for creating plotting applications. 我基本上是用于创建绘图应用程序的PySide的自定义版本。 The folder structure is as follows: 文件夹结构如下:

Juji/
    Images/
    juji/
    Tests/

Where the inner 'juji' folder contains the package. 内部“ juji”文件夹中包含软件包的位置。 The 'Images' folder contains icons which are used in the package and referenced relatively, eg: “图像”文件夹包含图标,这些图标在包装中使用并且相对引用,例如:

QtGui.QAction(QtGui.QIcon('Images/chart_edit.png'), 'Plot', self)

When I create an example UI script in the 'Tests' folder and import juji, everything works fine and the icons show. 当我在“测试”文件夹中创建示例UI脚本并导入juji时,一切正常,并显示图标。

If I create an external project and try to import 'juji' into it, the GUI shows, but none of the icon pictures show. 如果我创建一个外部项目并尝试将“ juji”导入到其中,则将显示GUI,但未显示任何图标图片。

I have tried moving the 'Images' folder inside the 'juji' folder, but this doesn't help. 我曾尝试将'Images'文件夹移至'juji'文件夹内,但这无济于事。

None of the icons are explicitly used outside of the package - surely it should not matter where my external project is relative to it? 没有一个图标在软件包外部明确使用-当然,与外部项目相对的位置无关紧要吗? (I have added it to the PYTHONPATH). (我已将其添加到PYTHONPATH中)。

Any ideas? 有任何想法吗?

I did a fix which is a bit cludgy: 我做了一个修正,有点笨拙:

I moved 'Images' inside juji and created a guidata.py file. 我将'Images'移至guidata.py内并创建了guidata.py文件。

In guidata , I get the absolute path from the __file__ attribute, remove the filename and append the 'Images' folder. guidata ,我从__file__属性获取了绝对路径,删除了文件名并附加了'Images'文件夹。

I can then import guidata to each module in the package and use the absolute path to get the icon files. 然后,我可以将guidata导入guidata中的每个模块,并使用绝对路径获取图标文件。 This seems to work with any other external project. 这似乎可以与任何其他外部项目一起使用。

It is a bit of a hack though. 不过,这有点骇人听闻。 A working relative filepath would be much neater, but I just cant seem to get it working. 一个有效的相对文件路径会更整洁,但我似乎无法使其正常工作。

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

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