2017-03-28 31 views
2

我正在使用cx_freeze將python文件傳輸到exe文件。問題是當我在setup.py中排除tkinter時,我可以成功生成exe文件,但是當執行exe文件時,它說No Module named tkinter如何在使用cx_freeze將腳本轉換爲.exe時包含tkinter?

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas", 
        "matplotlib","PIL"], "excludes": ["tkinter"]} 

,但是當我嘗試包括tkinter,它只是不能生成exe文件。

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas", 
        "matplotlib","PIL","tkinter"]} 
File "C:\Users\changchun_xu\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__ 
    raise KeyError(key) from None 
KeyError: 'TCL_LIBRARY' 
+0

你確定它說''沒有模塊命名爲tkinter「.'而不是'_tkinter'?也許還檢查[這](http://stackoverflow.com/questions/35533803/keyerror-tcl-library-when-i-use-cx-freeze)問題 – abccd

+0

感謝您的幫助!我再次查看信息,原始信息是「No module named'tkinter'」 – woniuwoniu

+0

也許你可以嘗試下面的內容[that](http://stackoverflow.com/questions/35533803/keyerror-tcl-library-when-通過輸入'os.environ ['TCL_LIBRARY'] = r'C:\ Program Files \ Python36-32 \ tcl \ tcl8.6''和 'os.environ,推薦使用i-use-cx-freeze/35706103) ['TK_LIBRARY'] = r'C:\ Program Files \ Python36-32 \ tcl \ tk8.6''在setup.py中。 **注意:用你實際的tcl和tk路徑替換路徑** – abccd

回答

7

你必須做出兩處修改您的setup.py把事情的工作:

  1. 設置TCL-LIBRARYTK_LIBRARY環境變量。 (你已經這樣做)

  2. 添加tcl86t.dlltk86t.dllinclude_files參數

所以setup.py應該是這個樣子:

import os 
from cx_Freeze import setup, Executable 

os.environ['TCL_LIBRARY'] = 'c:/python36/tcl/tcl8.6' 
os.environ['TK_LIBRARY'] = 'c:/python36/tcl/tk8.6' 

# Dependencies are automatically detected, but it might need 
# fine tuning. 
buildOptions = dict(
    packages = [], 
    excludes = [], 
    include_files=['c:/python36/DLLs/tcl86t.dll', 'c:/python36/DLLs/tk86t.dll'] 
) 

import sys 
base = 'Win32GUI' if sys.platform=='win32' else None 

executables = [ 
    Executable('editor.py', base=base) 
] 

setup(name='editor', 
     version = '1.0', 
     description = '', 
     options = dict(build_exe = buildOptions), 
     executables = executables) 
+0

感謝您的幫助!對於我的問題,似乎matplotlib調用「TK」,實際上我只是在我的python文件中添加matplotlib.use(「Agg」)將解決問題。下次如果我必須使用「TK」,我會嘗試您的建議。 – woniuwoniu

+0

,爲我工作,謝謝 – veritaS

+0

*缺少手冊!謝謝!* – veganaiZe