【问题标题】:cx_freeze Tkinter 'Module not Found'cx_freeze Tkinter '未找到模块'
【发布时间】:2025-12-29 00:30:14
【问题描述】:

我正在尝试从我的 python 脚本创建一个可执行文件。我正在使用 Windows 7、cx_freeze 5.0.2 和 Python 3.6。

我知道 Tkinter 不包含在普通库中,您需要添加类似于以下 2 行的内容:

os.environ['TCL_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tk8.6"

当然除了 3.6 和我所在的位置,但是我在 Anaconda 3.6 中找不到他们的目录

我创建了以下名为 setup.py 的文件

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"]}

# 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 = "McCabe-Thiele",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("GUI.py", base=base)])

并使用python setup.py bdist_msi 从cmd 行运行它。

它成功创建了 dist,然后成功安装。

但是,当我运行 .exe 时,会出现以下错误:

ModuleNotFoundError: no module named 'tkinter'

提前感谢您对此提供的任何帮助

【问题讨论】:

    标签: python tkinter cx-freeze


    【解决方案1】:

    在第三行添加,"includes":["tkinter"]

    会自动检测依赖关系,但可能需要微调。

    build_exe_options = {"packages": ["os"],"includes":["tkinter"]}
    

    当我使用 python setup.py build 运行它时,它对我有用

    来自问题的更新代码:

    import sys
    from cx_Freeze import setup, Executable
    
    # Dependencies are automatically detected, but it might need fine tuning.
    build_exe_options = {"packages": ["os"],"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 = "McCabe-Thiele",
            version = "0.1",
            description = "My GUI application!",
            options = {"build_exe": build_exe_options},
            executables = [Executable("GUI.py", base=base)])
    

    【讨论】: