在 py2exe 的 PyGTK 程序中显示图像

发布于 2024-10-07 05:52:30 字数 344 浏览 4 评论 0原文

我使用 PyGTK 在 Python 中编写了一个 GUI 应用程序,并希望将其作为可执行文件分发,为此我使用 py2exe。当我运行程序时,我收到以下警告,

GdkPixbuf-WARNING **: Cannot open pixbuf loader module file 'gtk-2.0\gdk-pixbuf.loaders': No such file or directory

并且即使 dist\etc\gtk-2.0\gdk-pixbuf.loaders 存在,我的图像也不会显示。

如何将我的程序打包为 Windows exe 并使用这些图像?

I have written a GUI app in Python using PyGTK and want to distribute it as an executable, for which I'm using py2exe. When I run my program, I get the following warning

GdkPixbuf-WARNING **: Cannot open pixbuf loader module file 'gtk-2.0\gdk-pixbuf.loaders': No such file or directory

and my images do not display, even though dist\etc\gtk-2.0\gdk-pixbuf.loaders exists.

How can I package my program as a Windows exe and use these imagines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-10-14 05:52:30

如果您使用 py2exe,则必须将 gtk+(等、共享、lib)打包到项目中与 exe 处于同一级别的位置:

示例目录结构:

PROGRAM.exe
LOT_OF_PYD_AND_LIB_FILES...
etc
etc\fonts
etc\gtk-2.0
etc\pango
lib
lib\gtk-2.0
share
share\aclocal
share\glib-2.0
share\locale
share\themes

您有该目录结构吗?

If you are using py2exe, you must pack gtk+ (etc, shared, lib) inside your project at the same level of your exe:

Sample directory structure:

PROGRAM.exe
LOT_OF_PYD_AND_LIB_FILES...
etc
etc\fonts
etc\gtk-2.0
etc\pango
lib
lib\gtk-2.0
share
share\aclocal
share\glib-2.0
share\locale
share\themes

Do you have that directory structure?

心的位置 2024-10-14 05:52:30

标准答案是将GTK+的bin等、share和lib目录复制到
项目的 dist 目录是正确的(但我看到有人破坏了 [1] 并且
列表中缺少 bin),毕竟:

  • PyGTK 只不过是一层薄薄的胶水,使得 GTK+ 可用于 Python
  • PyGObject 只不过是一层薄薄的胶水,使得 GObject、GIO、..可用于 Python
  • PyCairo 只不过是一层薄薄的胶水,使 Cairo 可用于 Python

至少,您需要所有 3 个绑定包,并且由于它们。
如果底层平台不可用(.dll 文件、
配置文件,简而言之:整个shebang)。这就是 GTK+ 的原因
bin等、share和lib需要复制到项目的dist目录中。

然而,我几年前发布到 [1] 的 setup.py 片段是微妙的
不完整。正确的版本应该是:

from distutils.core import setup
import py2exe

setup(
    name = 'handytool',
    description = 'Some handy tool',
    version = '2.0',
    zipfile = 'bin/library.zip',
    windows = [{'script': 'handytool.py',
                'dest_base': 'bin/handytool'}
              ],

    options = {'py2exe': {'packages':'encodings',
                          'includes': 'glib, gio, gobject, cairo, atk, pango, pangocairo, gtk'}
              }
)

记下 zipfiledest_base 的值。有了这些选项
你的.exe、一堆.pyd文件和library.zip都是在
py2exe 的 dist/bin 目录。然后当你将 GTK+ 的目录复制到 py2exe 的目录时
您的可执行文件位于 dist 目录中,紧邻 libgtk-win32-2.0.0.dll 等
本来应该如此。如果不执行上述操作,则 PATH 环境配置错误
变量可能会干扰您的 .dll 文件(有时不兼容)
py2exe 的可执行文件加载。

所以对于上面的setup.py文件,dist正确的目录结构应该是
看起来像这样:

bin/handytool.exe
bin/library.zip
bin/*.pyd (all .pyd files py2exe deemed needed)
bin/* (complete copy of GTK+ runtime bin dir)
etc/* (complete copy of GTK+ runtime etc dir)
share/* (complete copy of GTK+ runtime share dir)
lib/* (complete copy of GTK+ runtime lib dir)

当你使上述工作正常时,你会发现加载图像
就可以了,你可以开始考虑省略一些部分
共享/(例如您不想要/不需要的翻译文件)等

mvg,
迪特

[1] http://www.py2exe.org/index.cgi/Py2exeAndPyGTK

编辑 2011/07/05:修复了 PyGObject 2.28/PyGTK 2.24 的包含选项。
如果您仅使用 PyGObject 2.28,则包含选项具有
包含“glib、gio、gobject”。如果您使用 PyGTK 那么它需要:
'glib、gio、gobject、cairo、atk、pango、pangocairo、gtk'。

The standard answer of copying GTK+'s bin, etc, share and lib directory to the
project's dist directory is correct (but I see somebody has vandalized [1] and
bin is missing from the list), after all:

  • PyGTK is nothing more than a thin layer of glue making GTK+ available to Python
  • PyGObject is nothing more than a thin layer of glue making GObject, GIO, ... available to Python
  • PyCairo is nothing more than a thin layer of glue making Cairo available to Python

At a minimum, you need all 3 of those bindings packages and due to their
nature they are useless if the underlying platform is not available (.dll files,
configuration files, in short: the whole shebang). That's why GTK+'s
bin, etc, share and lib need to be copied to your project's dist directory.

The setup.py fragment I posted a couple of years ago to [1] is however subtly
incomplete. The correct version should have been:

from distutils.core import setup
import py2exe

setup(
    name = 'handytool',
    description = 'Some handy tool',
    version = '2.0',
    zipfile = 'bin/library.zip',
    windows = [{'script': 'handytool.py',
                'dest_base': 'bin/handytool'}
              ],

    options = {'py2exe': {'packages':'encodings',
                          'includes': 'glib, gio, gobject, cairo, atk, pango, pangocairo, gtk'}
              }
)

Take note of the values for zipfile and dest_base. With these options
your .exe, a bunch of .pyd files and library.zip are all created in
py2exe's dist/bin directory. Then when you copy GTK+'s directories to py2exe's
dist directory your executable lives right next to libgtk-win32-2.0.0.dll et al
as it should be. If you don't do the above, a misconfigured PATH environment
variable might interfere with what (sometimes incompatible) .dll files your
py2exe'd executable loads.

So for the above setup.py file, the correct directory structure of dist should
look like this:

bin/handytool.exe
bin/library.zip
bin/*.pyd (all .pyd files py2exe deemed needed)
bin/* (complete copy of GTK+ runtime bin dir)
etc/* (complete copy of GTK+ runtime etc dir)
share/* (complete copy of GTK+ runtime share dir)
lib/* (complete copy of GTK+ runtime lib dir)

When you get the above working correctly you will find that loading images
just works and you can start thinking about leaving out some parts of
share/ (like the translation files you don't want/need) etc.

mvg,
Dieter

[1] http://www.py2exe.org/index.cgi/Py2exeAndPyGTK

Edit 2011/07/05: fixed the includes option for PyGObject 2.28/PyGTK 2.24.
If you're only using PyGObject 2.28 the includes option has
to contain 'glib, gio, gobject'. If you're using PyGTK then it needs:
'glib, gio, gobject, cairo, atk, pango, pangocairo, gtk'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文