Comment Faire Fonctionner Cx_freeze Avec Tkinter
.png)
Okay, imagine this: I spent a whole weekend, fueled by questionable amounts of coffee, building this amazing little GUI app with Tkinter. It was going to be the next big thing, I swear! A revolutionary… uh… something. Anyway, it worked perfectly on my machine. Naturally. But then I tried to share it with my poor, unsuspecting Aunt Ginette, who still thinks the internet is a series of tubes. Boom. Crashing, errors, general digital chaos. She gave me that look. You know the one. The “You-call-yourself-a-tech-person?” look. Humiliating, right? Turns out, I needed to package it properly, and that’s where cx_Freeze comes to the rescue. (Mostly. Sometimes. Okay, often with a bit of wrestling.)
So, what's the deal with cx_Freeze and Tkinter anyway? Well, Tkinter is a GUI toolkit, and your Python script relies on it. When you share your script, the recipient's machine needs to have Tkinter and Python installed. cx_Freeze bundles up all those dependencies, including Python itself, into a neat little package that Aunt Ginette (and everyone else) can run without having to install a whole development environment. Think of it as a self-contained little ecosystem for your app. Pretty cool, huh?
Setting Up cx_Freeze
First things first: you need to install cx_Freeze. Open your terminal or command prompt and type:
Must Read
pip install cx_Freeze
Easy peasy. (Assuming pip is working properly, of course. If not, welcome to dependency hell! Just kidding… mostly.)

Now, the fun begins: creating a setup.py file. This file tells cx_Freeze what to include in your package. Here’s a basic example:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("your_script.py", base=base)]
setup(
name = "Your Awesome App", #Change this!
version = "1.0", #And this!
description = "Does amazing things!", #Don't forget this one too!
executables = executables
)
Replace your_script.py with the name of your main Python script. You’ll also want to customize the name, version, and description. Obvious, right? But you'd be surprised... (Seriously, do change those!)
Save this file as setup.py in the same directory as your Python script.
![[Résolu] Cx_Freeze avec python 3.5.1 par Dev0110 - page 1 - OpenClassrooms](https://s3-eu-west-1.amazonaws.com/sdz-upload/prod/upload/Cx_Freeze.png)
Building the Executable
Open your terminal, navigate to the directory containing setup.py, and run:
python setup.py build
cx_Freeze will churn away for a bit (or longer, depending on the complexity of your app), and then, ta-da! A build directory will appear. Inside, you'll find your executable and all its dependencies. Congratulations! You've just created a portable version of your Tkinter application.

Dealing With Tkinter-Specific Issues
Sometimes, cx_Freeze needs a little extra nudge to properly package Tkinter apps. One common issue is missing Tkinter DLLs (dynamic link libraries). If you encounter errors related to tcl86t.dll or similar files, you might need to explicitly include the Tkinter library directory in your setup.py file. Here’s how:
from cx_Freeze import setup, Executable
import os
PYTHON_INSTALL_DIR = os.path.dirname(os.__file__)
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6') #Replace 8.6 with your version
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6') #Replace 8.6 with your version
base = None
executables = [Executable("your_script.py", base=base)]
setup(
name = "Your Awesome App",
version = "1.0",
description = "Does amazing things!",
executables = executables,
options = {
"build_exe": {
"include_files": [
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), 'tcl86t.dll'), #Replace 8.6 with your version
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), 'tk86t.dll') #Replace 8.6 with your version
]
}
}
)
Important: Replace "8.6" with the actual version number of your Tcl/Tk installation. You can usually find this information by looking in your Python installation directory (usually in the 'tcl' subfolder).
Also, sometimes you might need to include other files, like images or data files that your application uses. You can add them to the include_files list in the setup.py file. For example:

"include_files": [("path/to/your/image.png", "image.png")]
This will copy image.png to the same directory as your executable.
Testing and Debugging
Before unleashing your creation upon the world (or Aunt Ginette), test it thoroughly! Run the executable from the build directory. If you encounter any errors, carefully read the error messages. They often provide clues about missing dependencies or incorrect paths. Remember that error messages are your friends... even if they don't feel like it at the time.
cx_Freeze can be a bit finicky, but with a little patience and persistence, you can get it working. And once you do, you'll be able to share your Tkinter creations with the world, or at least with Aunt Ginette, without causing digital mayhem.
