Windows上的PyGObject和cx_freeze

时间:2015-02-20 22:22:15

标签: python gtk cx-freeze pygobject gdk

我正试图"冻结"一个使用PyGObject和Gdk / Gtk的Python 3文件。这是Python脚本:

from gi.repository import Gtk, Gdk

class Handler:
    def onDeleteWindow(self, *args):
        Gtk.main_quit(*args)

    def searchPressed(self, button):
        print("Hello World!")

builder = Gtk.Builder()
builder.add_from_file("glade/test.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
style_provider = Gtk.CssProvider()
window.set_name("window1")
css = """
#window1 {
    background-color: #777777;
}
"""
style_provider.load_from_data(css.encode())
Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(), 
    style_provider,     
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
window.show_all()
Gtk.main()

这是我的setup.py:

import os, site, sys
from cx_Freeze import setup, Executable

## Get the site-package folder, not everybody will install
## Python into C:\PythonXX
site_dir = site.getsitepackages()[1]
include_dll_path = os.path.join(site_dir, "gnome")

## Collect the list of missing dll when cx_freeze builds the app
missing_dll = ['libgtk-3-0.dll',
               'libgdk-3-0.dll',
               'libatk-1.0-0.dll',
               'libcairo-gobject-2.dll',
               'libgdk_pixbuf-2.0-0.dll',
               'libjpeg-8.dll',
               'libpango-1.0-0.dll',
               'libpangocairo-1.0-0.dll',
               'libpangoft2-1.0-0.dll',
               'libpangowin32-1.0-0.dll',
]

## We also need to add the glade folder, cx_freeze will walk
## into it and copy all the necessary files
glade_folder = 'glade'

## We need to add all the libraries too (for themes, etc..)
gtk_libs = ['etc',
            'lib',
            'share',
            'lib/gdk-pixbuf-2.0',
            'lib/girepository-1.0',
            'share/glib-2.0'
]

## Create the list of includes as cx_freeze likes
include_files = []
for dll in missing_dll:
    include_files.append((os.path.join(include_dll_path, dll), dll))

## Let's add glade folder and files
include_files.append((glade_folder, glade_folder))

## Let's add gtk libraries folders and files
for lib in gtk_libs:
    include_files.append((os.path.join(include_dll_path, lib), lib))

base = None

## Lets not open the console while running the app
if sys.platform == "win32":
    base = "Win32GUI"

executables = [
    Executable("gtktest.py",
               base=base
    )
]

buildOptions = dict(
    compressed = False,
    includes = ["gi"],
    packages = ["gi"],
    include_files = include_files
    )

setup(
    name = "gtktest",
    author = "Monty Python",
    version = "1.0",
    description = "GTK test",
    options = dict(build_exe = buildOptions),
    executables = executables
)

这似乎可以成功构建。但是,当我运行可执行文件时,我收到此错误:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "gtktest.py", line 1, in <module>
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2212, in _find_and_load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 321, in _call_with_frames_removed
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
  File "C:\Python34\lib\site-packages\gi\__init__.py", line 42, in <module>
    from . import _gi
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2284, in _handle_fromlist
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 321, in _call_with_frames_removed
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
  File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
  File "ExtensionLoader_gi__gi.py", line 22, in <module>
  File "ExtensionLoader_gi__gi.py", line 14, in __bootstrap__
ImportError: DLL load failed: The specified module could not be found.

有人能指出我正确的方向来解决这个问题吗?我不知道自己哪里出错了,因为我试图包含所有丢失的DLL,但它仍然拒绝工作。或者,如果任何人都可以指出一种更好的方法将其打包为可执行文件,那也是值得赞赏的。我已经尝试过py2exe,但也没有运气。

提前致谢。

2 个答案:

答案 0 :(得分:1)

  1. 要知道确切缺少的dll,需要下载并安装:https://docs.microsoft.com/en-us/sysinternals/downloads/listdlls
  2. 您必须从c:\ pythonXX \ Lib \ site-packages \ gnome \ XXX.dll复制丢失的dll文件以构建\ exe_your_executable

答案 1 :(得分:0)

去年我遇到了同样的问题。很难猜出哪些DLL是必需的,哪些不是。然后我开始包含所有DLL,删除我不确定的那些,并检查程序是否仍在工作。据我记得,没有多少我可以删除。如果您仍然对一个有效的例子感兴趣,请查看以下内容:
https://github.com/da-mkay/subsynco/blob/master/scripts/dist_win/setup.py
(但它也使用GStreamer,因此包含了一些项目可能不需要的DLL)