import _tkinter # 如果失败,你的 Python 可能没有为 Tk 配置

import _tkinter # If this fails your Python may not be configured for Tk

一些初步信息: 我的电脑上有 windows 10,所有程序都是 64 位版本。

我正在使用 tkinter 在 python (3.6.1) 中编写游戏,现在我想将其转换为 .exe。我使用了 cx_freeze (5.0.1) 并且它进行了构建,但是当我尝试打开游戏时 window 打开然后立即关闭。因此我尝试通过cmd打开它并弹出以下错误:

File "sliks.py", line 1, in <module>
File "C:\Users\Tinka\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.

我已经检查了 tkinter 支持,如下所示: https://wiki.python.org/moin/TkInter 没有错误发生。

我也曾尝试使用 pip 安装 tk-dev,正如它在有关此主题的一些答案中所说的那样,但是当我收到此消息时没有任何反应:

C:\WINDOWS\system32>pip install tk-dev
Collecting tk-dev
Could not find a version that satisfies the requirement tk-dev (from versions: )
No matching distribution found for tk-dev

我的电脑上从来没有任何 python 2.x,所以没有像这种情况那样混淆的库:ImportError DLL load failed importing _tkinter

这是我用于 cx_freeze 的 setup.py 文件,以防出现问题:

from cx_Freeze import setup, Executable
import os

os.environ['TCL_LIBRARY'] = r'C:\Users\Tinka\AppData\Local\Programs\Python\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Tinka\AppData\Local\Programs\Python\Python36\tcl\tk8.6'

base = None

setup(
    name = "Six",
    version = "0.1",
    options = {"build_exe": {"packages": ["tkinter"]}},
    executables = [Executable("sliks.py", base=base)]
    )

任何想法可能是什么问题?我知道关于这个主题有很多未解决的问题,但我已经尝试了大部分解决方案,但没有成功。

我不得不非常努力地寻找自己的答案。不确定这是否对任何人有帮助,但对我有用。 据我了解,这些错误是在 cx_freeze 找不到所有依赖项或获取不正确的依赖项时生成的。

我做的第一件事是深入到我的 python 目录。在这里要非常小心,并确保您正在查看 python 代码的执行位置。如果您不知道,您的 IDE 可能会给您这条路。在多个安装或环境的情况下,您可能会关闭。

进入后,我确定了导致错误的文件。对于我的情况,这是一个 tkinter 依赖项。 tcl86.dll 和 tk86.dll 是问题所在。你可以看到我添加的行。然后我的标志实际上开始做,所以我不得不添加它。现在效果很好。这是我的 setup.py 文件(cx_freeze 配置)的示例。

from cx_Freeze import setup, Executable
import sys
import os

includes = []
include_files = [r"C:\Users\Ace\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll",
                 r"C:\Users\Ace\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll",
                 r"C:\Users\Ace\Desktop\IPNV\KP_App\FML\logo1.gif"]
os.environ['TCL_LIBRARY'] = r'C:\Users\Ace\AppData\Local\Programs\Python\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\Ace\AppData\Local\Programs\Python\Python36\tcl\tk8.6'
base = 'Win32GUI' if sys.platform == 'win32' else None


setup(name='KpApp', version='0.9', description='KP Report App',
      options={"build_exe": {"includes": includes, "include_files": include_files}},
      executables=[Executable(r'C:\Users\Ace\Desktop\IPNV\KP_App\FML\firstapp.py', base=base)])