Search code examples
pythonpython-3.xtkintercx-freeze

Issue when freezing a py script with cx_freeze


I just starred coding in Python and wanted to create a .exe standalone using cx freeze but I face a problem with tkinter. I was able to produce a very simple window but when I add tkinter, it does not work anymore.

Here are my codes:

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

Then in the cmd, this is what I do: I go to C:\Python34 and hit python.exe "Scripts\cxfreeze" "Scripts\tkinter2.py" It seems to compile but says that some modules are missing which seems to be tkinter. If I launch the .exe which is created, I have "ImportError: no module name 'Tkinter'". I am using Python 3.4 and have installed the corresponding cx_freeze.

Would you have an idea why I have such error? Is it because some underlying component of tkinter cannot be used when freezing my py script?

Thanks, StaP


Solution

  • Generally when using CX_Freeze you'll create a setup.py file (you can rename it to whatever you want). Then to build you would generally do 'python setup.py build' (this is in command prompt and you would cd to the directory the setup.py and the tkinter2.py file are stored). Most common reason Tk fails to build with cx freeze is you're missing the DLL files. Make a setup.py with the below code and try it out:

    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
    )
    

    edit: I also noticed you don't have a TK.mainloop() at the end of your program