使用cx_freeze将python脚本构建到exe文件

问题描述 投票:0回答:1

我试图将我的python脚本转换为exe文件,任何人都可以从任何计算机运行它,包括没有python的计算机。所以我看到一些指南解释了最好的方法是在cx_freeze库中使用。所以我构建了一个仅在tkinter中使用的小gui应用程序,这是我的代码:

import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()

这是我的设置文件:

from cx_Freeze import setup, Executable
setup(
    name="GUI PROGRAM",
    version="0.1",
    description="MyEXE",
    executables=[Executable("try.py", base="Win32GUI")],
    )

我运行此命令:

python setup.py build

然后我得到这个错误:

KeyError: 'TCL_LIBRARY

它只会在我使用tkinter时发生。所以我想我想念一些东西,我需要以某种方式添加tkinter到安装文件。有人能帮我吗?非常感谢你们。

python user-interface exe cx-freeze
1个回答
0
投票

尝试将您的设置脚本更改为:

from cx_Freeze import setup, Executable
import os
import sys
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR,'tcl','tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}

setup(
    name="GUI PROGRAM",
    version="0.1",
    description="MyEXE",
    options = {"build_exe": files},
    executables=[Executable("try.py", base="Win32GUI")],
)

os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR,'tcl','tcl8.6') os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')将删除错误消息,而files = {"include_files": ["<Path to Python>/Python36-32/DLLs/tcl86t.dll", "<Path To Python>/Python36-32/DLLs/tk86t.dll"], "packages": ["tkinter"]}将包括丢失的Tk和Tcl运行时。

© www.soinside.com 2019 - 2024. All rights reserved.