问题描述
一些初始信息:
我的计算机上装有Windows 10,所有程序都是64位版本。
Some initial information:I have windows 10 on my computer and all programs are 64-bit versions.
我正在用python编写游戏(3.6.1)使用tkinter,现在我想将其转换为.exe。我使用了cx_freeze(5.0.1)并进行了构建,但是当我尝试打开游戏时,会打开一个窗口,然后立即关闭。因此,我尝试通过cmd打开它,并弹出以下错误:
I'm writting a game in python (3.6.1) using tkinter and now I would like to convert it to .exe. I have used cx_freeze (5.0.1) and it made the build, but when I try to open the game a window opens and then closes immediately. Therefore I tried to open it via cmd and the following error pops up:
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支持,如下所示:
并且没有发生错误。
I have checked tkinter support as it says here:https://wiki.python.org/moin/TkInterand no error occurs.
我也尝试用pip安装tk-dev,因为它在此问题的一些答案中说过,但是当我收到此消息时什么也没有发生:
Also I have tried to install tk-dev with pip as it says in some answers on this subject but nothing happens as I get this message:
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,因此没有混合库,例如:
I never had any python 2.x on my computer so there are no mixed up libraries as in this case: ImportError DLL load failed importing _tkinter
这是我用于cx_freeze的setup.py文件,以防出现问题:
Here is my setup.py file I have used for cx_freeze in case there is something wrong with that:
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)]
)
任何想法可能是什么问题?我知道在这个问题上有很多悬而未决的问题,但是我尝试了大多数解决方案,但是没有运气。
Any ideas what could be the problem? I know there are many opened questions on this subject, but I have tried most of the solutions and had no luck.
推荐答案
I必须非常努力地为自己弄清楚这一点。不知道这是否对任何人都有用,但对我有用。
据我了解,当cx_freeze找不到所有依赖项或捕获了不正确的依赖项时,就会生成这些错误。
I had to scour pretty hard to figure this one out for myself. Not sure if this helps anyone but it worked for me.From what I understand these errors are generated when cx_freeze either cannot find all the dependencies or is grabbing incorrect ones.
我要做的第一件事是深入到我的python目录中。在这里要非常小心,并确保您正在查找正在执行python代码的位置。如果您不知道,IDE可能会为您提供此路径。如果有多个安装或环境,则可能会关闭。
First thing I did was to drill down into my python directory. Be VERY careful here and ensure you are looking where your python code is executing. Your IDE may give you this path if you do not know it. In cases of multiple installations or environments you may be off.
在那里,我确定了引起错误的文件。就我的情况而言,这是对tkinter的依赖。问题是tcl86.dll和tk86.dll。您可以看到我添加的行。然后我的徽标实际上开始执行此操作,因此我必须添加它。现在效果很好。这是我的setup.py文件(cx_freeze配置)的样本。
Once in there I identified which file was causing the error. For my situation it was a tkinter dependency. tcl86.dll and tk86.dll were the problem. You can see the lines I added. Then my logo actually started doing it so I had to add that. Now it works great. Here is a sample of my setup.py file (cx_freeze config).
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)])
这篇关于import _tkinter#如果失败,则可能没有为Tk配置Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!