Cx_Freeze 构建没有错误但 exe 打不开

Cx_Freeze building without error but exe doesn't open

我正在尝试使用 cx_Freeze 构建一个 exe,它使用多个模块:

import tkinter as tk
from tkinter import ttk
import random, time, bluetooth, json, sys, os
from _thread import *
from threading import Thread, Lock

当我尝试构建 exe 时,它​​似乎运行良好:它没有引发任何错误并创建了包含 exe 文件的构建文件夹。但是,当我尝试打开 exe 文件时,它根本打不开。如果短暂地闪烁 windows 但随后消失。我的 setup.py 是这样的:

from cx_Freeze import setup,Executable
import sys
import os

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

includes = []
include_files = []
packages = []
base = "Win32GUI"
setup(
    name = 'Buzzer',
    version = '0.1',
    description = 'Buzzer application',
    author = 'Me',
    executables = [Executable('Buzzer.py')]
)

闪烁的屏幕包含以下回溯:

Traceback (most recent call last): File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run module.run() File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run exec(code, m.dict) File "print.py", line 1, in File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 36, in import _tkinter # If this fails your Python may not be configured for Tk ImportError: DLL load failed: The specified module could not be found.

您需要告诉 cx_Freeze 在构建目录中包含 TCL 和 TK DLL。

对于cx_Freeze 5.1.1(当前版本)或5.1.0,DLL 需要包含在构建目录的lib 子目录中。您可以通过将元组 (source, destination) 传递给 include_files 列表选项的相应条目来做到这一点:

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

您需要将此 include_files 列表传递给设置调用中的 build_exe 选项(您还应该将您定义的 base 传递给 Executable ):

setup(
    name = 'Buzzer',
    version = '0.1',
    description = 'Buzzer application',
    author = 'Me',
    options={'build_exe': {'include_files': include_files}},
    executables = [Executable('Buzzer.py', base=base)]
)

对于其他 cx_Freeze 版本,DLL 需要直接包含在构建目录中。这可以通过以下方式完成:

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