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
Hide the console of an .exe file created with PyInstaller in Tkinter
When creating executable files from Tkinter applications using PyInstaller, you may notice a command console window appears alongside your GUI. This console can be hidden using the --windowed flag in PyInstaller.
The Problem
By default, PyInstaller creates executables that show a console window. For GUI applications like Tkinter apps, this console is unnecessary and can look unprofessional.
Solution: Using the --windowed Flag
To hide the console window, use the following PyInstaller command ?
pyinstaller --onefile app.py --windowed
The --windowed flag tells PyInstaller to create a windowed application without a console.
Example Application
Let's create a simple Tkinter application to demonstrate this ?
app.py
# Import the required libraries
from tkinter import *
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry
win.geometry("700x350")
# Set the default color of the window
win.config(bg='#aad5df')
def display_text():
Label(win, text="Hello World!", background='white', foreground='purple1').pack()
Button(win, text="Click Me", background="white", foreground="black",
font=('Helvetica', 13, 'bold'), command=display_text).pack(pady=50)
win.mainloop()
Creating the Executable
Open terminal in the same directory as your Python file and run ?
pyinstaller --onefile app.py --windowed
This command creates:
-
--onefile: Single executable file -
--windowed: No console window
PyInstaller will generate an app.exe file in the dist folder.
Alternative Flags
You can also use these equivalent options ?
pyinstaller --onefile app.py --noconsole pyinstaller --onefile app.py -w
All three flags (--windowed, --noconsole, -w) achieve the same result.
Output
When you run the executable from the dist folder, it opens directly as a GUI application without showing any console window ?
Conclusion
Use the --windowed flag with PyInstaller to hide the console window when creating executable files from Tkinter applications. This creates a cleaner, more professional user experience for GUI applications.
