【问题标题】:Error after converting tkinter program to exe using cx_Freeze and running the exe file使用 cx_Freeze 将 tkinter 程序转换为 exe 并运行 exe 文件后出错
【发布时间】:2018-12-18 16:19:03
【问题描述】:

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

所以,如果您知道什么可能/导致错误,请告诉我!

【问题讨论】:

    标签: python python-3.x operating-system exe cx-freeze


    【解决方案1】:

    (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'))]
    )
    

    【讨论】:

    • 我按照你说的做了,现在我得到了一个不同的错误。因此,我将设置文件和错误报告图像从我的问题编辑为现在的样子。
    • @Goodester 我已根据您修改后的问题编辑了我的答案。如果可能的话,也请把错误信息作为文本而不是图像。
    猜你喜欢
    • 2020-07-11
    • 2018-06-07
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多