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

时间:2018-12-18 16:19:03

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

我已经花费了数小时试图找到解决此问题的方法,但是还没有找到任何有用的方法。 所以我正在尝试使用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()

因此,如果您有什么想法会导致错误,请告诉我!

1 个答案:

答案 0 :(得分:0)

(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'))]
)
相关问题