Creating a Standalone Python Executable with Tkinter GUI – Tutorial 40

Posted by


In this tutorial, we will learn how to create a standalone Python EXE executable for a Python Tkinter GUI application. This will allow you to distribute your GUI application to users who do not have Python installed on their systems.

Step 1: Install the necessary libraries
First, you will need to install two libraries: pyinstaller and pywin32. Pyinstaller is a tool that can convert Python scripts into standalone executables, and pywin32 is a library that enables Pyinstaller to work correctly with Tkinter.

You can install these libraries using pip by running the following commands in your terminal:

pip install pyinstaller
pip install pywin32

Step 2: Create your Python Tkinter GUI application
Next, you will need to create your Python Tkinter GUI application. For the purposes of this tutorial, we will create a simple calculator application. Here is an example code for a basic calculator GUI:

import tkinter as tk

def calculate():
    result.set(eval(expression.get()))

root = tk.Tk()
root.title("Calculator")

expression = tk.StringVar()
result = tk.StringVar()

entry = tk.Entry(root, textvariable=expression)
entry.pack()

button = tk.Button(root, text="Calculate", command=calculate)
button.pack()

label = tk.Label(root, textvariable=result)
label.pack()

root.mainloop()

Save this code in a file named calculator.py.

Step 3: Create the executable
Now that you have created your Python Tkinter GUI application, you can use Pyinstaller to create a standalone executable. In your terminal, navigate to the directory where your calculator.py file is located and run the following command:

pyinstaller --onefile --windowed calculator.py

This command tells Pyinstaller to create a standalone executable with a single file (--onefile) and to hide the console window (--windowed).

After running this command, Pyinstaller will generate a dist folder in the same directory as your Python script. Inside this folder, you will find the standalone executable file for your GUI application.

Step 4: Distribute your executable
You can now distribute your standalone executable to users who do not have Python installed on their systems. Simply share the executable file with them, and they will be able to run your Python Tkinter GUI application without any additional setup.

In conclusion, creating a standalone Python EXE executable for a Python Tkinter GUI application is a straightforward process that allows you to easily share your GUI applications with others. By following the steps outlined in this tutorial, you can create and distribute standalone executables for your Tkinter applications with ease.

0 0 votes
Article Rating
42 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@nashonmumo5679
30 days ago

This is awesome. Like and subscribed to my best presentation ever!

@Ev-001
30 days ago

Do you have a video of how to compile multiple .py into an exe, I also am using CTk as well. I tried pyinstaller but its missing dependencies

@wickedsnuk3812
30 days ago

You are on windows right ? in mac it doesnt bring all the necessary files like this, I mean I cannot share it with others

@goforward684
30 days ago

i save the file as .pyw and run pyinstaller with window defender had to be turned off before finding this video 😭

@xith-eb1jeevanraj.l485
30 days ago

Sir ,I can't access the e.get ,it show nonetype attribute error

@willisthehy
30 days ago

you can do –noconsole will also have the cmd not pop up!

@pverprogramz641
30 days ago

I am making an exe that checks if python is installed in the computer and i am making the exe in python.. is it possible ?? if so how ?

@lemartien25000
30 days ago

Thanks for answering

@lemartien25000
30 days ago

Well, but if you have to add a database for example tree_crm.db to the exe..how can we proceed in pyinstaller ?

@AB-cd5gd
30 days ago

How to fix windows defender flag it as a virus

@paweszapawaau119
30 days ago

not received

@NeooNovaa
30 days ago

The file that I installed needed SQLite database to work, and when I run the project I installed it can't connect to the database, how do I fix this?

@shubhamchidrewar4767
30 days ago

I am getting Invalid syntax error.
not sure why this is happening

@rogerperez9856
30 days ago

Hello, I have created an app in Python which must connect to a database, but I want to extract the credentials from the code and make an executable to put it into production. The problem is that one way or another the user has access to the credentials, whether they put them in the code or include the .env file with the executable. Anyway, I don't know what to do to prevent users from having access to the credentials, I would like to know how I can do so that only the app has access to the credentials. Greetings.

@PablockDA
30 days ago

Is there any fix nowaday to get rid of the virus alert this cause wich is not a virus the .exe i mean

@goulio3679
30 days ago

Thanks for the explanation. For some reason after installing pyinstaller through pip, I keep getting "No module named 'pyinstaller'"

@clausarberg
30 days ago

-w

@sunnypepple6571
30 days ago

Where can I distribut my GUI App?

@sunnypepple6571
30 days ago

Or possibly how can I host my GUI app?

@sunnypepple6571
30 days ago

Where do I host my Tkinter GUI App?