Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include tkinter when using cx_freeze to convert script to .exe?

I am using cx_freeze to transfer a python file to a exe. the problem is when i exclude tkinter in the setup.py, i can generate the exe file successfully, but when execute the exe file, it says No Module named tkinter.

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

but when I try to include tkinter, it just can't generate the exe file.

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'
like image 594
woniuwoniu Avatar asked Mar 28 '17 02:03

woniuwoniu


1 Answers

You have to make two modifications to your setup.py to get things working:

  1. Set TCL-LIBRARY and TK_LIBRARY environment variables. (You already did this)

  2. Add the tcl86t.dll and tk86t.dll to your include_files parameter

So the setup.py should look something like this:

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)
like image 58
MrLeeh Avatar answered Oct 13 '22 11:10

MrLeeh