cx_freeze in python 3.6
问题描述:
我已经在python 3.6中编写了一个代码,并且希望为该代码创建一个exe文件。我为此使用了cx_freeze。代码没有错,但我认为问题在setup.py文件中。这里是我的代码:cx_freeze in python 3.6
from tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()
我cx_freeze setup.py文件是这样的:
# setup file for cx_freeze
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
includes = ["atexit", "re"]
build_exe_options = {"packages": ["os", "tkinter"], "includes": includes}
setup(
name = "testing",
version = "1.0",
description = "Testing of tkinter",
options = {"build_exe": build_exe_options},
executables = [Executable("testing.py", base = base)]
)
当我转换我的脚本成exe文件,我得到了下面的错误。任何人都可以告诉我setup.py文件有什么问题吗?
running build
running build_exe
Traceback (most recent call last):
File "setup.py", line 20, in <module>
executables = [Executable("tk1.py", base = base)]
File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
distutils.core.setup(**attrs)
File "C:\Python36\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python36\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python36\lib\distutils\command\build.py", line 135, in run
self.run_command(cmd_name)
File "C:\Python36\lib\distutils\cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 219, in run
freezer.Freeze()
File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
self.finder = self._GetModuleFinder()
File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 340, in _GetModuleFinder
finder.IncludePackage(name)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 653, in IncludePackage
module = self._ImportModule(name, deferredImports)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 310, in _ImportModule
deferredImports, namespace = namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 403, in _InternalImportModule
parentModule, namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 416, in _LoadModule
namespace)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
self._LoadModule(name, fp, path, info, deferredImports, parent)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
self._RunHook("load", module.name, module)
File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
method(self, *args)
File "C:\Python36\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
tclSourceDir = os.environ["TCL_LIBRARY"]
File "C:\Python36\lib\os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'
,当我在我的setup.py文件添加以下两行,但我的脚本转换为EXE。
os.environ['TCL_LIBRARY'] = r'C:\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Python36\tcl\tk8.6'
但是当我运行exe文件时,我得到了图片中显示的以下错误。
答
你已经得到了的Tcl/Tk库文件夹,但你只是缺少运行时间或DLL。
您还需要使用include_files
参数来获取运行时。
如果您提供的路径是正确的,那么它们应该都位于C:/python36/DLLs/
。您希望tcl86t.dll
和tk86t.dll
都包含在您的设置脚本中。这很简单:
build_exe_options = {"packages": ["os", "tkinter"], "includes": includes, "include_files": ["C:/python36/DLLs/tcl86t.dll", "C:/python36/DLLs/tk86t.dll"]}
This answer also relates to your problem。
希望我有帮助。对于迟到的回答,我只是发现了这个问题。
这不能是Python 3.6的代码,因为'Tkinter'在Python 3中被重命名为'tkinter'。 – skrx
对不起,请再次检查错误。 @skrx –
[如何在使用cx_freeze将脚本转换为.exe时包含tkinter的可能的副本](https://*.com/questions/43059408/how-to-include-tkinter-when-using-cx-freeze -to-convert-script-to-exe) – Kos