Let us suppose we have created a tkinter application and now, we want to bundle the standalone application to make it portable and executable. We can use different Python packages that support various functionality to bundle the whole application code into an executable installer. These packages compress the code and convert the standalone application into an executable code.
For a Windows-based user, we can use py2exe; for Linux, we can use Freeze; and for Mac, we can use py2app.
Example
In this example, we have created a Windows-based application that prints “Hello World” on the screen. Initially, we will create a setup.py file in the same directory where the main application file exists. Then, we will extend the functionality of the main application file by passing the filename as the parameter in the setup.py.
setup.py
from distutils.core import setup import py2exe setup(console=['main_app.py'])
Now, type and run setup.py with the py2exe package in the command shell. After running the command, it will create a dist folder in the same directory which contains an executable file named as “main_app.exe”.
main_app.py
#Import the tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x400") def present(): Label(win, text="Hello World", font=('Times New Roman bold',30)).pack(pady=10) Button(win, text="Click Me", command= present).pack(pady=20) win.mainloop()
Output
Running the main_app.exe file will open the following window with a Button and a Label in it.