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

Leave a Reply

42 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@nashonmumo5679
2 hours ago

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

@Ev-001
2 hours 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
2 hours 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
2 hours 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
2 hours ago

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

@willisthehy
2 hours ago

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

@pverprogramz641
2 hours 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
2 hours ago

Thanks for answering

@lemartien25000
2 hours 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
2 hours ago

How to fix windows defender flag it as a virus

@paweszapawaau119
2 hours ago

not received

@NeooNovaa
2 hours 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
2 hours ago

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

@rogerperez9856
2 hours 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
2 hours 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
2 hours ago

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

@clausarberg
2 hours ago

-w

@sunnypepple6571
2 hours ago

Where can I distribut my GUI App?

@sunnypepple6571
2 hours ago

Or possibly how can I host my GUI app?

@sunnypepple6571
2 hours ago

Where do I host my Tkinter GUI App?

42
0
Would love your thoughts, please comment.x
()
x