tkinter程序使用cx_Freeze编译,但程序无法启动

时间:2018-10-12 19:06:11

标签: python python-3.x tkinter cx-freeze

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

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

进行一些调整后,我可以编译该项目,但是当我单击.exe时,将触发加载鼠标的动画,但没有任何加载。之前曾问过这个问题,但从未解决过。

Where to start looking in the code when your .exe doesn't work after 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

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

3 个答案:

答案 0 :(得分: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')]

相反。

另请参阅Getting "ImportError: DLL load failed: The specified module could not be found" when using cx_Freeze even with tcl86t.dll and tk86t.dll added inpython tkinter exe built with cx_Freeze for windows won't show GUI

编辑:

另一个问题是cx_Freeze有一个python 3.7的错误,尚未得到纠正。参见Cx_freeze crashing Python3.7.0。您可以找到一个错误修复程序的链接,您应该手动应用该错误修复程序(根据可解决此问题的OP,请参见注释)。

答案 1 :(得分:1)

在尝试了一个更简单的hello world示例写入控制台后(同样失败),我偶然发现了罪魁祸首。

What could be the reason for fatal python error:initfsencoding:unable to load the file system codec?

使用找到的代码here更新了freezer.py文件并使用jpeg提供的setup.py后,我的示例应用程序开始运行。谢谢你们的迅速回应。

答案 2 :(得分:0)

我在这里有一个有效的setup.py。也许您可以尝试使用相同的配置后看看它是否有效。基本上,有时在编译后,tk和tcl dll /程序包会丢失,因此在安装过程中需要包括它们。

import sys, os
from cx_Freeze import setup, Executable

includes = []

include_files = [r"C:\Users\user\AppData\Local\Programs\Python\Python36-32\DLLs\tcl86t.dll",
                 r"C:\Users\user\AppData\Local\Programs\Python\Python36-32\DLLs\tk86t.dll"]

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

os.environ['TCL_LIBRARY'] = r'C:\Users\user\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\user\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'

setup(name="simple_Tkinter",
      version="0.1",
      options={"build_exe":{"includes":[],"include_files":include_files}},
      description="Sample cx_Freeze Tkinter script",
      executables=[Executable("SimpleTkApp.py",base=base)])