我花了几个小时试图找到如何解决这个问题,但还没有找到任何有用的东西。因此,我尝试使用cx_Freeze将tkinter程序转换为exe。在我尝试打开实际的exe文件之前,一切都很好。这是错误报告。
我的设置文件:
import os
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
os.environ['TCL_LIBRARY'] = r"C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36DLLstcl86t.dll"
os.environ['TK_LIBRARY'] = r"C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36DLLstk86t.dll"
build_options = dict(
packages=['sys'],
includes=['tkinter'],
include_files=[(r'C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36DLLstcl86t.dll',
os.path.join('lib', 'tcl86t.dll')),
(r'C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36DLLstk86t.dll',
os.path.join('lib', 'tk86t.dll'))]
)
executables = [
Executable('app.py', base=base)
]
setup(name='simple_Tkinter',
options=dict(build_exe=build_options),
version='0.1',
description='Sample cx_Freeze tkinter script',
executables=executables,
)
和我的脚本:
import tkinter as tk
root = tk.Tk()
tk.Label(root, text='Application', font='Tahoma 15 bold italic').pack()
tk.mainloop()
所以,如果你知道是什么原因导致了这个错误,请告诉我!
(OP修改问题后编辑的答案(
我想os.environ
的定义有问题。它们应该指向TCL/TK目录,而不是DLL。这些定义应该读起来像:
os.environ['TCL_LIBRARY'] = r"C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36tcltcl8.6"
os.environ['TK_LIBRARY'] = r"C:UsersOsborne-Win10AppDataLocalProgramsPythonPython36tcltk8.6"
无论如何,最好让设置脚本动态地找到TCL/TK资源的位置,如以下答案所示:
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
build_options = dict(
packages=['sys'],
includes=['tkinter'],
include_files=[(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
os.path.join('lib', 'tcl86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join('lib', 'tk86t.dll'))]
)