使用 cx_freeze 为 tkinter 接口创建 .exe 文件

Posted

技术标签:

【中文标题】使用 cx_freeze 为 tkinter 接口创建 .exe 文件【英文标题】:creating .exe file with cx_freeze for a tkinter interface 【发布时间】:2014-02-25 04:37:20 【问题描述】:

我到处寻找这个答案,但我找不到答案。我有一个与 tkinter 有接口的 python 脚本(3.3)。我使用 cx_freeze 创建了一个可执行文件,得到了一个包含一些文件和文件夹的构建文件夹。我双击 .exe 文件,没有任何反应。我正在使用以下设置:

import sys

from cx_Freeze import setup, Executable



base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
        name = "simple_Tkinter",
        version = "0.1",
        description = "Sample cx_Freeze Tkinter script",
        executables = [Executable("the timer.py", base = base)])

如果我只是打开我的代码并运行它,界面就会完美运行。在构建时我没有收到任何错误消息(至少我看不到任何错误消息......顺便说一句,我如何验证这一点?)。关于问题可能是什么的任何想法?或任何其他替代模块?

谢谢!! :)

【问题讨论】:

如果在命令提示符下运行 exe,会发生什么? 【参考方案1】:

我会对此发表评论,但我还没有声誉......

编译输出/日志中是否有任何警告/错误?

在命令提示符下运行可执行文件时会发生什么?

您的可执行文件是否需要 cx_freeze 找不到的库?

您可能需要指定其他选项,例如包含的库... 调整 cx_freeze documentation 中的示例,您可以指定包含 TKinter:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = "includes": ["tkinter"]

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(
    name = "simple_Tkinter",
    version = "0.1",
    description = "Sample cx_Freeze Tkinter script",
    options = "build_exe": build_exe_options,
    executables = [Executable("the timer.py", base = base)])

setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        options = "build_exe": build_exe_options,
        executables = [Executable("guifoo.py", base=base)])

我知道我在让 py2exe 与 PySide/PyQt4、matplotlib、numpy 等一起工作时遇到了很多有趣的问题。一些模块,比如 matplotlib,甚至提供了一种方法来列出构建/分发所需的所有数据文件应用程序(matplotlib.get_py2exe_datafiles())。 Enthought 的 TraitsUI 解决方案利用 glob 来获取所需文件的目录。我的观点是,因为模块导入在某些库中可能是动态的、混乱的或黑魔法的,所以许多构建实用程序无法找到所有必需的资源。此外,一旦您的可执行文件正在运行,如果您在发行版中发现您知道您的应用程序不需要的东西,您可以使用其他选项将其排除,这有助于减少发行版中的膨胀。希望 TKinter 不会太难开始工作 - *** 上的其他人似乎很成功。

很抱歉,我没有可靠的解决方案,但我会尽力提供帮助!祝你好运!

【讨论】:

非常感谢您的回答。发现了问题,感觉有点笨……最后没加root.mainloop()……是菜鸟的错误,不过build_exe_options部分我一定会牢记!再次感谢!! :)

以上是关于使用 cx_freeze 为 tkinter 接口创建 .exe 文件的主要内容,如果未能解决你的问题,请参考以下文章

将 cx_freeze 与 tkinter 一起使用时出错

tkinter 程序使用 cx_Freeze 编译,但程序不会启动

带有 cx_freeze 的 python 3.2 tkinter 图标

cx_freeze 错误:找不到模块 tkinter

cx_Freeze 没有名为“Tkinter”的模块

使用 cx_Freeze 创建 exe 或 msi 时使用 Tkinter tix 的 Python 错误