2016-04-18 69 views
1

我剛剛在Python中加星標編碼,並希望使用cx freeze創建一個.exe單機版,但我面臨tkinter的問題。我能夠生成一個非常簡單的窗口,但是當我添加tkinter時,它不再工作。使用cx_freeze凍結py腳本時出現問題

這裏是我的代碼:

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

然後在CMD,這是我做的: 我去C:\ Python34和命中python.exe「腳本\ cxfreeze「」Scripts \ tkinter2.py「 它似乎編譯,但說,一些模塊丟失似乎是tkinter。如果我啓動創建的.exe,我有「ImportError:沒有模塊名稱'Tkinter'」。 我正在使用Python 3.4並安裝了相應的cx_freeze。

你知道我爲什麼有這樣的錯誤嗎?是否因爲凍結我的py腳本時不能使用tkinter的某個基礎組件?

感謝, STAP

回答

0

通常使用CX_Freeze您將創建一個setup.py文件時(你可以將其重命名爲任何你想要的)。然後建立你通常會做'python setup.py build'(這是在命令提示符下,你將cd到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()