Python:CX_Freeze 构建后问题

Python: CX_Freeze issue after build

我创建了一个小型转换器,并在使用 CX_Freeze 构建它后显示此错误

回溯(最近调用最后): 文件 "C:\users\LDC\AppData\Local\Programs\python\python36-32\lib\sitr\e-packages\cx_freeze\initscripts_startup_.py",第 14 行 运行 module.run() 文件 "C:\users\LDC\AppData\Local\Programs\python\python36-32\lib\sitr\e-packages\cx_freeze\initscripts\console.py", line26 in 运行 exec(code,m.dict) 文件"GUI1.py",第 1 行,在 文件 "C:\USERS\LDC\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON36-32\LIB\TKINTER_INIT_.PY",第36行,在 import_tkinter#如果失败,您的 python 可能没有为 Tk 配置 ImportError:DLL 加载失败:找不到指定的模块

This is a screen shot from the error

现在这是我的代码:

from tkinter import *
window1=Tk()

def convert():
    var2=var1.get()
    var3=var2*3.785
    e2.insert(0,var3)

def clear():
    e1.delete(0,END)
    e2.delete(0,END)

def quit():
    window1.destroy()

var1=IntVar()
label1=Label(window1,text='Gallons',padx=25).grid(row=0,sticky=W)
e1=Entry(window1,width=25,textvariable=var1)
e1.grid(row=0,column=1)
label2=Label(window1,text='Liters',padx=25).grid(row=1,sticky=W)
e2=Entry(window1,width=25)
e2.grid(row=1,column=1)

window1.title("Converter")
window1.geometry("400x200+200+200")
button1= Button(text='convert',command=convert,width=15,).grid(row=4,column=0)
button2= Button(text='clear',command=clear,width=15).grid(row=4,column=1)
button3= Button(text='exit',command=quit,width=15).grid(row=5,column=1)

mymenu=Menu()
mymenu.add_cascade(label='File')
mymenu.add_cascade(label='Edit')
mymenu.add_cascade(label='View')
mymenu.add_cascade(label='Tools')
mymenu.add_cascade(label='Help')
window1.config(menu=mymenu)

window1.mainloop()

这是设置代码

import cx_Freeze
import sys
import os.path
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')

base = None

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

executables = [cx_Freeze.Executable("GUI1.py", base=base, icon="clienticon.ico")]

cx_Freeze.setup(
    name = "GUI1",
    options = {"build_exe": {"packages":["tkinter"], "include_files":["clienticon.ico"]}},
    version = "0.01",
    description = "Ya Rb",
    executables = executables
    )

我尝试了以下但没有成功: 1.卸载cx freeze并重新安装 2. 尝试不同版本的 python .. python 2.7 3. 尝试使用 py2exe 和 pyinstaller 得到不同的错误 4. 还要确保环境中的python路径设置正确

在此先感谢您的帮助..

这个错误并没有看起来那么严重。您只需要知道 Python 安装路径即可。

错误的含义:您包含了 tkinter 库但忘记了 tkinter 运行 次(tk86t.dll 和 tcl86t.dll)。为了使您的脚本正常工作,您需要包含它们。

这可以通过使用 include_files 语句来完成。快速搜索安装显示它们位于名为 DLLs 的文件夹中。我们需要为安装脚本提供文件路径和文件名。可以这样做:

  "include_files":["<path to python>/Python36-32/DLLs/tcl86t.dll","<path to python>/Python36-32/DLLs/tk86t.dll"]

现在可以使用了。

您的设置脚本将如下所示:

import cx_Freeze
import sys
import os.path
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')

base = None

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

executables = [cx_Freeze.Executable("GUI1.py", base=base, icon="clienticon.ico")]

cx_Freeze.setup(
    name = "GUI1",
    options = {"build_exe": {"packages":["tkinter"], "include_files":["clienticon.ico", "<path to python>/Python36-32/DLLs/tcl86t.dll","<path to python>/Python36-32/DLLs/tk86t.dll"]}},
    version = "0.01",
    description = "Ya Rb",
    executables = executables
    )