Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compile a Python 3 app to an .exe using Tkinter?
Converting a Python Tkinter application to an executable (.exe) file allows you to distribute your application without requiring Python installation on the target machine. PyInstaller is the most popular tool for creating standalone executables from Python applications.
Installation and Setup
First, install PyInstaller using pip ?
pip install pyinstaller
Creating the Tkinter Application
Let's create a simple Tkinter application that we'll convert to an executable file ?
# Import the required libraries
from tkinter import *
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry
win.geometry("750x350")
win.title("TutorialsPoint Demo")
def display_text():
Label(win, text="Hey There! Welcome to TutorialsPoint",
font=('Helvetica', 22, 'bold'),
foreground="navy").pack()
# Create a Button
Button(win, text="Click Me",
font=('Helvetica', 13, 'bold'),
foreground="OrangeRed3",
background="White",
command=display_text).pack(pady=50)
win.mainloop()
Save this code as app.py in your desired directory.
Compilation Process
Follow these steps to compile your Tkinter application into an executable file ?
Step 1: Navigate to Application Directory
Open Command Prompt or Terminal and navigate to the directory containing your app.py file ?
cd path/to/your/application
Step 2: Run PyInstaller
Execute the following command to create a single executable file ?
pyinstaller --onefile app.py
Step 3: Locate the Executable
PyInstaller creates several folders. Navigate to the dist folder to find your executable file ?
dist/ app.exe # Your executable file
PyInstaller Options
Here are some useful PyInstaller options for Tkinter applications ?
| Option | Description | Usage |
|---|---|---|
--onefile |
Creates a single executable file | Recommended for distribution |
--windowed |
Hides console window | Essential for GUI apps |
--icon=icon.ico |
Sets custom icon | Professional appearance |
--name=MyApp |
Sets executable name | Custom naming |
Recommended Command for Tkinter Apps
For GUI applications, use this command to hide the console window ?
pyinstaller --onefile --windowed app.py
Common Issues and Solutions
Missing Modules
If your application uses additional packages, PyInstaller might miss them. Use the --hidden-import option ?
pyinstaller --onefile --hidden-import=module_name app.py
Large File Size
Executable files can be large because they include the entire Python interpreter. This is normal for standalone applications.
Testing the Executable
Once created, test your executable file ?
Navigate to the
distfolderDouble-click
app.exeto run itVerify all functionality works as expected
Test on a machine without Python installed
Conclusion
PyInstaller effectively converts Tkinter applications to standalone executables. Use --onefile --windowed for GUI applications to create professional, distributable files. Always test executables on target systems to ensure compatibility.
