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 create a directly-executable cross-platform GUI app using Python(Tkinter)?
Python is a programming language that can create cross-platform applications for Microsoft Windows, Mac OS, and Linux. To build GUI applications, we use the Tkinter library, and to convert them into executable files, we use packaging tools.
Available Tools for Creating Executables
Different tools are available for different operating systems ?
For Windows executables ? PyInstaller, py2exe
For Linux executables ? Freeze
For Mac executables ? py2app
PyInstaller is the most popular choice as it works across all platforms.
Installing PyInstaller
First, install PyInstaller using pip ?
pip install pyInstaller
Creating a Sample GUI Application
Let's create a simple Tkinter application that allows users to select a day from a dropdown menu ?
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Add a Label
Label(win, text="Select a Day from the Menu",
font=('Arial', 13)).pack(pady=10)
# Create a Variable to store the selection
var = StringVar()
# Create an OptionMenu Widget and add choices to it
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
option = OptionMenu(win, var, *days)
option.config(bg="gray81", fg="white")
option['menu'].config(bg="green3")
option.pack(padx=20, pady=30)
# Add a button to show selected day
def show_selection():
if var.get():
result_label.config(text=f"You selected: {var.get()}")
Button(win, text="Show Selection", command=show_selection).pack(pady=10)
result_label = Label(win, text="", font=('Arial', 11))
result_label.pack()
win.mainloop()
Save this code as app.py.
Converting to Executable File
Open the command prompt in the directory containing your Python file and run ?
pyinstaller app.py
This command creates several folders ?
dist/app/ ? Contains the executable file and all dependencies
build/ ? Contains temporary build files
app.spec ? Configuration file for PyInstaller
Creating a Single File Executable
To create a single executable file instead of a folder, use the --onefile option ?
pyinstaller --onefile app.py
This creates a single app.exe file in the dist/ folder that can be run independently.
Additional PyInstaller Options
| Option | Description |
|---|---|
--windowed |
Hide console window (GUI apps only) |
--icon=icon.ico |
Add custom icon to executable |
--name=myapp |
Set custom name for executable |
Complete Build Command Example
pyinstaller --onefile --windowed --name=DaySelector app.py
Conclusion
PyInstaller makes it easy to convert Python Tkinter applications into executable files that run on any system without requiring Python installation. Use --onefile for single-file distribution and --windowed for GUI applications.
