使用 cx_Freeze 和 运行 exe 文件将 tkinter 程序转换为 exe 后出错
Error after converting tkinter program to exe using cx_Freeze and running the exe file
我花了几个小时试图找到解决这个问题的方法,但我还没有找到任何有用的东西。
所以我正在尝试使用 cx_Freeze 将 tkinter 程序转换为 exe。一切正常,直到我尝试打开实际的 exe 文件 Here's is the error report。
我的安装文件:
import os
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
os.environ['TCL_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll"
os.environ['TK_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll"
build_options = dict(
packages=['sys'],
includes=['tkinter'],
include_files=[(r'C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll',
os.path.join('lib', 'tcl86t.dll')),
(r'C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tk86t.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()
因此,如果您知道 could/is 导致错误的原因,请告诉我!
(OP修改问题后编辑的答案)
我猜 os.environ
定义有问题。它们应该指向 TCL/TK 目录,而不是 DLL。这些定义应该是这样的:
os.environ['TCL_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\tcl\tk8.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'))]
)
我花了几个小时试图找到解决这个问题的方法,但我还没有找到任何有用的东西。 所以我正在尝试使用 cx_Freeze 将 tkinter 程序转换为 exe。一切正常,直到我尝试打开实际的 exe 文件 Here's is the error report。
我的安装文件:
import os
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
os.environ['TCL_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll"
os.environ['TK_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll"
build_options = dict(
packages=['sys'],
includes=['tkinter'],
include_files=[(r'C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll',
os.path.join('lib', 'tcl86t.dll')),
(r'C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\DLLs\tk86t.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()
因此,如果您知道 could/is 导致错误的原因,请告诉我!
(OP修改问题后编辑的答案)
我猜 os.environ
定义有问题。它们应该指向 TCL/TK 目录,而不是 DLL。这些定义应该是这样的:
os.environ['TCL_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\tcl\tcl8.6"
os.environ['TK_LIBRARY'] = r"C:\Users\Osborne-Win10\AppData\Local\Programs\Python\Python36\tcl\tk8.6"
无论如何,让安装脚本按照
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'))]
)