【问题标题】:Error on running tkinter exe made with cx_freeze运行使用 cx_freeze 制作的 tkinter exe 时出错
【发布时间】:2016-09-22 05:08:22
【问题描述】:

我正在尝试使用 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:

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

也许我应该说 Widgets_tmp.py 中的第 1 行如下所示:

import tkinter as tk

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

我也尝试过使用:

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

但没有运气。

【问题讨论】:

  • 也许您需要为tkinter 使用--include-modules 命令行参数。见stackoverflow.com/questions/2223128/…
  • 这个问题让我想到尝试将 tkinter 添加到包含选项和包中,但这没有用。我对编码和再次使用命令行都很陌生。不过,DOS 的记忆又回来了。我应该简单地将 --include-modules 添加到“python Widgets_tmp.py Build”命令中还是需要以某种方式指定 tkinter?
  • 我理解链接问题的答案的方式是您需要包含 tkinter 模块...您可以通过使用 --include-modules 命令行选项或通过放置includes = setup.py 脚本中的行。
  • 刚找到问题creating .exe file with cx_freeze for a tkinter interface。请注意tkinter 是如何在已接受答案的代码中的build_exe_options 行中指定的。
  • 我尝试使用options={"build_exe": {"includes": ["tkinter"]}},,但结果相同。我还尝试了 options={"build_exe": {"packages": ["tkinter"]}},正如上面代码中所注释的那样。那里也一样。因为我现在正在工作,所以我一回到家就会尝试使用命令行选项。也许我的includes 做错了什么,但我似乎找不到它。

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


【解决方案1】:

通过修改我的 PC 上的 setup.py 正确运行了一个 tkinter exe。

操作系统 = 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)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多