How to compile a Python 3 app to an .exe using Tkinter?


Python is well-known for its rich library of extensions and packages. We can import and install the necessary packages from the library. However, if we require to run a Tkinter application with an executable file in Windows Operating System, then we can use the Pyinstaller package in Python. It converts a Python-based application to a native executable file (or.exe).

Follow the steps to compile a Tkinter-based application into an executable file,

  • Install Pyinstaller using 'pip install pyinstaller'.

  • Open the Command or Shell in the same directory where the application file is located and run the file using the command, pyinstaller --onefile app.py. It will create necessary folders such as binaries and other source files.

  • Go to > dist folder where the executable file of the application is located.

  • Run the .exe file.

Example

app.py

#Import the required libraries
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry
win.geometry("750x350")

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

Output

The .exe file will be created in the dist folder, as shown below.

Running the executable file of the application will display a window with a button on it.

Upon clicking the "Click Me" button, it will show a text label in the same window.

Updated on: 25-May-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements