首页 > 解决方案 > tkinter 程序使用 cx_Freeze 编译,但程序不会启动

问题描述

我正在尝试按照本教程创建一个可执行文件

https://github.com/anthony-tuininga/cx_Freeze/tree/master/cx_Freeze/samples/Tkinter

经过一些调整后,我可以编译该项目,但是当我单击 .exe 时,鼠标加载动画会触发,但没有加载任何内容。以前有人问过这个问题,但从未得到解决。

当您的 .exe 在 cx_freeze 之后无法工作时,从哪里开始查看代码?

我的应用文件

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title('Button')
print("something")
new = messagebox.showinfo("Title", "A tk messagebox")
root.mainloop()

我的 setup.py

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [
    Executable('SimpleTkApp.py', base=base)
]

setup(name='simple_Tkinter',
      version='0.1',
      description='Sample cx_Freeze Tkinter script',
      executables= [Executable("SimpleTkApp.py", base=base)])

我也一直在手动添加 TCL/TK 库

set TK_LIBRARY=C:\...\tk8.6  etc

我的配置:python 3.7、cx_Freeze 5.1.1

任何帮助将不胜感激,我什至不知道从哪里开始。

标签: pythonpython-3.xtkintercx-freeze

解决方案


尝试setup.py如下修改你:

import sys
from cx_Freeze import setup, Executable

import os
PYTHON_INSTALL_DIR = os.path.dirname(sys.executable)
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')

include_files = [(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
                 (os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll'))]

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [Executable('SimpleTkApp.py', base=base)]

setup(name='simple_Tkinter',
      version='0.1',
      description='Sample cx_Freeze Tkinter script',
      options={'build_exe': {'include_files': include_files}},
      executables=executables)

这应该适用于cx_Freeze版本 5.1.1(当前版本)。在此版本中,包含的模块位于lib构建目录的子目录中。如果您使用 5.0.1 或更早的版本,请设置

include_files = [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
                 os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll')]

反而。

另请参阅使用 cx_Freeze 时出现“ImportError: DLL load failed: The specified module could not be found”,即使添加了 tcl86t.dll 和 tk86t.dll并且使用 cx_Freeze for windows 构建的 python tkinter exe 不会显示 GUI

编辑:

另一个问题是cx_Freezepython 3.7 有一个尚未纠正的错误。请参阅Cx_freeze 使 Python3.7.0 崩溃。您可以在那里找到一个指向您应该手动应用的错误修复的链接(根据 OP 这解决了问题,请参阅评论)。


推荐阅读