使用cx_freeze运行tkinter exe时出错

时间:2016-05-24 20:02:29

标签: python windows tkinter cx-freeze python-3.5

我正在尝试使用cx_Freeze从Python 3.5中编写的脚本创建一个独立的exe,以便能够在没有Python的计算机上运行它。

程序本身只是一个小型计算器,具有使用tkinter的简单UI。 我正在使用Windows 10。

我创建了一个看起来像这样的安装脚本。

import sys
from cx_Freeze import setup, Executable
# replaces commandline arg 'build'
sys.argv.append("build")

filename = "Widgets_tmp.py"
base = None
if sys.platform == "win32":
    base = "Win32GUI"
setup(
    name = "Widgets",
    version = "1.0",
#   options={"build_exe": {"packages": ["tkinter"]}},
    description = "cx_Freeze Tkinter script",
    executables = [Executable(filename, base=base)])

在我尝试运行exe之前,这没有问题。然后我收到了这个错误:

Traceback (most recent call last):
  File "C:\python64\lib\site-packages\cx_freeze\initscripts\Console.py"
line 21, in <module>
    exec(code, m.__dict__)
  File "Widgets_tmp.py", line 1, in <module>
  File "C:\python64\lib\tkinter\__init__.py", line 35, in <module>
    import _tkinter#If this fails you Python may not be configured for Tk
ImportError: DLL load failed:

我尝试在代码中注释掉的代码和“includes”而不是“packages”中包含tkinter manualy,但结果相同。

也许我应该说Widgets_tmp.py中的第1行看起来像这样:

import tkinter as tk

我尝试从源代码中冻结示例代码但得到相同的错误。这两个代码都可以使用python完美运行。

我也尝试过使用:

options={"build_exe": {"includes": ["tkinter"]}},

但没有运气。

1 个答案:

答案 0 :(得分:4)

通过在我的电脑上修改setup.py来正确运行tkinter exe。

OS = win7,Python = 3.5.2,cx_freeze = 5.0

setup.py:

includes      = []
include_files = [r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tcl86t.dll", \
                 r"C:\Users\(USERNAME)\AppData\Local\Programs\Python\Python35-32\DLLs\tk86t.dll"]

setup(
    name = "Test",
    version = "1.0",
    options = {"build_exe": {"includes": includes, "include_files": include_files}},
    executables = [Executable("test.py", base=base)]
)

您必须修改(USERNAME)您的环境。