Python:构建

时间:2018-01-05 16:45:01

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

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

追踪(最近一次通话): 运行module.run()中的文件" C:\ users \ LDC \ AppData \ Local \ Programs \ python \ python36-32 \ lib \ sitr \ e-packages \ cx_freeze \ initscripts_startup_.py",line14 文件" C:\ users \ LDC \ AppData \ Local \ Programs \ python \ python36-32 \ lib \ sitr \ e-packages \ cx_freeze \ initscripts \ console.py",run26运行exec(代码,米。字典) 文件" GUI1.py",第1行,in 文件 " C:\用户\ LDC \ APPDATA \ LOCAL \程序\ PYTHON \ PYTHON36-32 \ LIB \ TKINTER_INIT_.PY",line36,在 import_tkinter#如果失败,则可能没有为Tk配置python 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冻结并重新安装 2.尝试了不同版本的python .. python 2.7 3.试图使用py2exe和pyinstaller得到不同的错误 4.还确保环境中的python路径设置正确

先谢谢你的帮助..

1 个答案:

答案 0 :(得分:1)

此错误并不像看起来那么糟糕。您只需要知道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
    )