Python 使用cx\U冻结py脚本时出现问题

Python 使用cx\U冻结py脚本时出现问题,python,python-3.x,tkinter,cx-freeze,Python,Python 3.x,Tkinter,Cx Freeze,我刚刚开始用Python编写代码,并希望使用cx freeze创建一个独立的.exe,但tkinter有一个问题。我能够生成一个非常简单的窗口,但当我添加tkinter时,它不再工作了 这是我的密码: tkinter2.py: #!/usr/bin/env python # -*-coding:Latin-1 -* import tkinter base = None if sys.platform == 'win32': ba

我刚刚开始用Python编写代码,并希望使用cx freeze创建一个独立的.exe,但tkinter有一个问题。我能够生成一个非常简单的窗口,但当我添加tkinter时,它不再工作了

这是我的密码:

tkinter2.py:

    #!/usr/bin/env python
    # -*-coding:Latin-1 -*


    import tkinter 

    base = None

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

    TK=Tk()

    # Function called when user hit the keyboard
    def clavier(event):
        global coords

        touche = event.keysym

        if touche == "Up":
            coords = (coords[0], coords[1] - 10)
        elif touche == "Down":
            coords = (coords[0], coords[1] + 10)
        elif touche == "Right":
            coords = (coords[0] + 10, coords[1])
        elif touche == "Left":
            coords = (coords[0] -10, coords[1])
        # change of coordinates for the rectangle
        canvas.coords(rectangle, coords[0], coords[1], coords[0]+25, coords[1]+25)

    # canvas creation
    canvas = Canvas(TK, width=250, height=250, bg="ivory")
    # initial coord
    coords = (0, 0)
    #rectangle creation
    rectangle = canvas.create_rectangle(0,0,25,25,fill="violet")
    canvas.focus_set()
    canvas.bind("<Key>", clavier)
    # canvas creation
    canvas.pack()
#/usr/bin/env python
#-*-编码:拉丁语-1-*
进口tkinter
基本=无
如果sys.platform==“win32”:
base=“Win32GUI”
TK=TK()
#当用户点击键盘时调用的函数
def键盘(事件):
全球协调
touche=event.keysym
如果触摸==“向上”:
coords=(coords[0],coords[1]-10)
elif touche==“向下”:
coords=(coords[0],coords[1]+10)
elif touche==“右”:
coords=(coords[0]+10,coords[1])
elif touche==“左”:
coords=(coords[0]-10,coords[1])
#更改矩形的坐标
canvas.coords(矩形,coords[0],coords[1],coords[0]+25,coords[1]+25)
#画布创作
画布=画布(TK,宽度=250,高度=250,bg=“象牙”)
#初始坐标
坐标=(0,0)
#矩形创建
矩形=画布。创建矩形(0,0,25,25,fill=“violet”)
canvas.focus_set()
canvas.bind(“,键盘)
#画布创作
canvas.pack()
然后在cmd中,我会这样做: 我转到C:\Python34并点击python.exe“Scripts\cxfreeze”“Scripts\tkinter2.py” 它似乎在编译,但说一些模块丢失了,这似乎是tkinter。如果我启动创建的.exe,我有“ImportError:没有模块名'Tkinter'”。 我使用的是Python3.4,并安装了相应的cx\U冻结

你知道我为什么会犯这样的错误吗?是不是因为冻结py脚本时无法使用tkinter的某些底层组件

谢谢,
StaP

通常在使用CX\u Freeze时,您将创建一个setup.py文件(您可以将其重命名为任何您想要的名称)。然后,要进行构建,通常需要执行“python setup.py build”(这是在命令提示符下,您需要将setup.py和tkinter2.py文件存储到目录中)。Tk无法使用cx冻结构建的最常见原因是缺少DLL文件。使用以下代码创建setup.py并进行尝试:

import sys
import cx_Freeze
import os.path


base = None

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


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

executables = [cx_Freeze.Executable("tkinter2.py", base=base)]


options = {
    'build_exe': {

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


         ],
    },

}

cx_Freeze.setup(
    name = "Stack Overflow Q",
    options = options,
    version = "1.0",
    description = 'Stack Overflow Q',
    executables = executables
)

编辑:我还注意到您的程序末尾没有TK.mainloop()

通常在使用CX\u Freeze时,您会创建一个setup.py文件(您可以将其重命名为任何您想要的名称)。然后,要进行构建,通常需要执行“python setup.py build”(这是在命令提示符下,您需要将setup.py和tkinter2.py文件存储到目录中)。Tk无法使用cx冻结构建的最常见原因是缺少DLL文件。使用以下代码创建setup.py并进行尝试:

import sys
import cx_Freeze
import os.path


base = None

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


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

executables = [cx_Freeze.Executable("tkinter2.py", base=base)]


options = {
    'build_exe': {

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


         ],
    },

}

cx_Freeze.setup(
    name = "Stack Overflow Q",
    options = options,
    version = "1.0",
    description = 'Stack Overflow Q',
    executables = executables
)
编辑:我还注意到程序末尾没有TK.mainloop()