python - 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
任何帮助将不胜感激,我什至不知道从哪里开始。
解决方案
尝试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_Freeze
python 3.7 有一个尚未纠正的错误。请参阅Cx_freeze 使 Python3.7.0 崩溃。您可以在那里找到一个指向您应该手动应用的错误修复的链接(根据 OP 这解决了问题,请参阅评论)。
推荐阅读
- java - 在控制器方法调用之前将 OAuth2 用户属性映射到 Object
- sql - postgreSQL 中经理的直接和间接报告
- python-3.x - python2的unicode(v,errors ='ignore')的确切python3等价物是什么?
- reactjs - Laravel Sanctum 与 Redux 工具包和 Axios
- apache-spark - Spark SQL Window 函数 - 需要手动重新分区吗?
- maps - 您如何集成 Google 地图 API 密钥?
- android - React Native:应用程序图标无缘无故地被调整大小
- php - easyPost,生成fedex取件,无法处理请求
- c# - 在单个 XElement 中使用 XPath 仅查找具有具有特定属性的子节点的节点
- sql - 将值从一列拆分到另一列 SQL DEVELOPER