Hide the console of an .exe file created with PyInstaller in Tkinter

TkinterPythonGUI-Programming

To convert a standard Tkinter application into a window executable file, we generally use thePyintsaller package. It converts the application file into an executable application. However, we notice that when we open the executable (or .exe) file, it displays a command shell before opening the application window. We can hide or avoid the console by specifying pyinstaller --oneline filename --windowed command.

Example

In this example, we will create an .exe file of the following program using PyInstaller.

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

Now, open the terminal in the same location where you have saved app.py and run the following command −

> pyinstaller –onefile app.py –windowed

It will create an app.exe file in the Dist folder.

Output

When we run the executable file located in the Dist folder, it will display a window with a button and a Label widget.

Notice that the .exe file didn't display the command shell before opening the application window.

raja
Updated on 25-May-2021 08:56:17

Advertisements