gpt4 book ai didi

python - 使用 cx_Freeze 将 tkinter 程序转换为 exe 并运行 exe 文件后出错

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:01 24 4
gpt4 key购买 nike

我花了几个小时试图找到如何解决这个问题,但我还没有找到任何有用的东西。所以我尝试使用 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()

因此,如果您知道可能/正在导致错误的原因,请告诉我!

最佳答案

(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"

无论如何,最好让安装脚本按照 this answer 中的建议动态查找 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'))]
)

关于python - 使用 cx_Freeze 将 tkinter 程序转换为 exe 并运行 exe 文件后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com