使用cx_freeze冻结的pyGTK应用看起来不同

2 投票
3 回答
664 浏览
提问于 2025-04-17 13:54

我的应用程序在使用 cx_freeze 冻结后看起来和用 Python 解释器运行时不一样(我用 Gui2Exe 来创建和运行 cx_freeze 脚本)。

[因为我不能发图片,这里是 UI 的链接,编辑一下?]

脚本运行:

从命令行运行的应用程序

冻结运行:

经过 cx_freeze 冻结后的应用程序

我尝试过在 cx_freeze 脚本中包含和不包含清单文件,但我不确定是什么原因导致应用程序的界面变化这么大。

这是 cx_freeze 脚本:

# ======================================================== #
# File automagically generated by GUI2Exe version 0.5.3
# Copyright: (c) 2007-2012 Andrea Gavana
# ======================================================== #

# Let's start with some default (for me) imports...

from cx_Freeze import setup, Executable



# Process the includes, excludes and packages first

includes = ['ast', 'gobject', 'gtk']
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter']
packages = ['BeautifulSoup', 'mechanize', 'pygtk']
path = []

# This is a place where the user custom code may go. You can do almost
# whatever you want, even modify the data_files, includes and friends
# here as long as they have the same variable name that the setup call
# below is expecting.

# No custom code added

# The setup for cx_Freeze is different from py2exe. Here I am going to
# use the Python class Executable from cx_Freeze


GUI2Exe_Target_1 = Executable(
    # what to build
    script = "moodle-downloader.py",
    initScript = None,
    base = 'Win32GUI',
    targetDir = r"md",
    targetName = "moodle-downloader.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = True,
    appendScriptToLibrary = True,
    icon = r"C:\Users\Nasser.Al-Hilal\Dropbox\CodeN\Projects\Applications\Personal\MoodleDownloader\res\md.ico"
    )


# That's serious now: we have all (or almost all) the options cx_Freeze
# supports. I put them all even if some of them are usually defaulted
# and not used. Some of them I didn't even know about.

setup(

    version = "0.3",
    description = "An app to assist in downloading assignment submissions from Moodle LMS.",
    author = "Nasser Al-Hilal",
    name = "Moodle Downloader",

    options = {"build_exe": {"includes": includes,
                             "excludes": excludes,
                             "packages": packages,
                             "path": path
                             }
               },

    executables = [GUI2Exe_Target_1]
    )

# This is a place where any post-compile code may go.
# You can add as much code as you want, which can be used, for example,
# to clean up your folders or to do some particular post-compilation
# actions.

# No post-compilation code added


# And we are done. That's a setup script :-D

我希望能让应用程序在运行时的样子和用解释器运行时一样。

3 个回答

0

这只是对Blasito回答的翻译:

“为了让这个正常工作,首先需要在你的电脑上下载并安装GTK-runtime。然后找到GTK-runtime的安装文件夹(在程序文件夹里),把里面的lib和share文件夹复制到生成的构建文件夹里。最后,把安装目录下的bin文件夹里的内容复制到构建文件夹。”

我可以确认这样做是有效的。

0

我的安装环境:

  • 操作系统:Windows 7
  • Python版本:2.7.9
  • GTK2(通过命令 python -m pip install pygtk 安装)
    • 安装的库包括:pycairo (1.8.10)、pygobject (2.28.3)、pygoocanvas (0.14.2)、pygtk (2.24.0)、pygtksourceview (2.10.1)
  • cx-Freeze(通过命令 python -m pip install cx-Freeze 安装)
    • cx-Freeze版本:4.3.4

cx-Freeze的编译命令:

 \Py279\Scripts\cxfreeze  MyGUIapp.py --target-dir somewheredir --base-name=Win32GUI

(其实,这一切都是在没有任何设置命令文件的情况下完成的,虽然可以创建一些 :))

一旦cx-Freeze编译成exe文件并复制它认为需要的dll文件,我就会得到一种motif/x11的外观和感觉。但是,当我把以下所有.dll文件添加到somewheredir时:

\Py279\Lib\site-packages\gtk-2.0\runtime\lib\gtk-2.0\2.10.0\engines\

到:

somewheredir\lib\gtk-2.0\2.10.0\engines\

我就会得到Windows的外观和感觉。

在我的情况下,找到的文件有:

  • libpixmap.dll
  • libsvg.dll
  • libwimp.dll

就这些。

另外:

通过在用户的主文件夹c:\Users\myuser中创建.gtkrc-2.0文件,可以进一步自定义设计。具体来说,可以影响默认的字体样式和大小、窗口的背景颜色等等。例如,以下代码(来自其他地方的提示)

    style "win32-font" {
      # make default font larger
      font_name = "Sans 12"
      # set the background to a light grey
      bg[NORMAL] = "#f6f6f6"
    }
    class "*" style "win32-font"

放在c:\Users\myuser\.gtkrc-2.0中,将会改变pygtk应用程序的“默认”字体大小,并改变用户运行的顶部窗口背景(这意味着每个用户都可以在这里设置自己的偏好)。

2

要让你能正常工作,你需要先下载gtkruntime,然后把它安装到你的电脑上。接着,把lib文件夹复制到你的项目中,并分享你生成的构建文件夹。然后,把gtkruntime里的bin文件夹的内容复制到你的构建文件夹里,这样你就可以开始工作了。

撰写回答