Python 运行用cx\U冻结生成的tkinter exe时出错

Python 运行用cx\U冻结生成的tkinter exe时出错,python,windows,tkinter,cx-freeze,python-3.5,Python,Windows,Tkinter,Cx Freeze,Python 3.5,我正在尝试使用cx\u Freeze从Python 3.5中编写的脚本创建一个独立的exe,以便能够在没有Python的计算机上运行它 该程序本身只是一个小计算器,具有使用tkinter的简单UI。 我正在使用Windows10 我创建了一个如下所示的安装脚本 import sys from cx_Freeze import setup, Executable # replaces commandline arg 'build' sys.argv.append("build") filenam

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

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

我创建了一个如下所示的安装脚本

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:

我试图冻结源代码中的示例代码,但得到了相同的错误。这两个代码在使用python时都能完美地工作

我还尝试使用:

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

但是运气不好。

通过修改我电脑上的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)]
)

您必须为您的环境修改(用户名)。

通过修改我的电脑上的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)]
)

您必须为您的环境修改(用户名)。

也许您需要使用
--include modules
命令行参数作为
tkinter
。看,这个问题给了我一个想法,尝试将tkinter添加到includes选项和包中,但这不起作用。我对编码和使用命令行都很陌生。然而,对DOS的记忆又回来了。我是否应该简单地将--include模块添加到“pythonwidgets\u tmp.py构建”中命令或我是否需要以某种方式指定tkinter?我理解链接问题的答案的方式是,您需要包含
tkinter
模块…您可以使用
--include modules
命令行选项,或者在setup.py脚本中添加
includes=
行。刚刚找到问题。注意在接受的答案中的代码中,
build\u exe\u options
行是如何指定
tkinter
的。我尝试了
options={“build\u exe”:{“includes”:[“tkinter”]},
,但结果相同。我还尝试了options={“build_exe”:{“packages”:[“tkinter”]},正如上面代码中注释的那样。同样的事情。我一回到家就会尝试使用命令行选项,因为我现在正在工作。也许我的
includes
有问题,但我似乎找不到。也许你需要使用
--includes modules
命令行参数作为
tkinter
。看,这个问题给了我一个想法,尝试将tkinter添加到includes选项和包中,但这不起作用。我对编码和使用命令行都很陌生。然而,对DOS的记忆又回来了。我是否应该简单地将--include模块添加到“pythonwidgets\u tmp.py构建”中命令或我是否需要以某种方式指定tkinter?我理解链接问题的答案的方式是,您需要包含
tkinter
模块…您可以使用
--include modules
命令行选项,或者在setup.py脚本中添加
includes=
行。刚刚找到问题。注意在接受的答案中的代码中,
build\u exe\u options
行是如何指定
tkinter
的。我尝试了
options={“build\u exe”:{“includes”:[“tkinter”]},
,但结果相同。我还尝试了options={“build_exe”:{“packages”:[“tkinter”]},正如上面代码中注释的那样。同样的事情。我一回到家就会尝试使用命令行选项,因为我现在正在工作。也许我的
includes
有问题,但我似乎找不到。