avatarBetter Everything

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2823

Abstract

</pre></div><p id="47bb">The above script opens a GUI (Graphical User Interface) by using the standard package: tkinter. This serves as a nice example because it gives us something visual: a GUI screen that opens on our screen.</p><p id="9c89">In the same directory I put an icon. An icon is an image with the <code>.ico</code> file extension. The icon will be what we click to start the app.</p><figure id="ce59"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*abD2hcELN-P8wghtxJohGA.png"><figcaption>The ico file that we will use as the icon of our dekstop app. Source: own image.</figcaption></figure><p id="9498">If you want to create your own icon you can read how I transformed a PNG image to an icon image here:</p><div id="0b56" class="link-block"> <a href="https://readmedium.com/converting-images-to-other-file-types-small-python-projects-3-c0aa6911ef79"> <div> <div> <h2>Converting Images to other File Types — Small Python projects #3</h2> <div><h3>Different scenarios require different image types. Learn how to convert images to other file types like: PNG, JPG or…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*699T6hNgFt3SEgdaN-HsTA.jpeg)"></div> </div> </div> </a> </div><h2 id="4e68">Step 4 — Use a pyinstaller command to transform a Python file into an executable file</h2><p id="02c1">Open the command line again and navigate to the directory where your script and icon are.</p><p id="bab9">You can navigate to a certain directory with the <code>cd</code> command (cd for change directory).</p><p id="c548">For example: <code>cd C:\Users\Better_Everything\Documents\Desktop App</code></p><p id="2e57">In that directory run the following command, in which you have to replace the filenames of the icon and the Python file to the names you used:</p><p id="fc88"><code>pyinstaller -F -w -i good_app.ico main.py</code></p><ul><li>pyinstaller is the package we installed in step 2 that transforms Python files into executable files</li><li>-F is to make sure our project gets combined into 1 file</li><li>-w is to make sure our program gets run without a command line screen opening</li><li>-i good_app.ico is to tell pyinstaller to use my image with filename <code>good_app.ico</code> as the icon for the executable file</li><li>main.py is the name of my Python file</li></ul><h2 id="9227">Step 5 — Clean Up and Testing</h2><p id="28db">After running the command in step 4, two directories should be created: <code>dist</code> and <code>build</code>. In the <code>dist</code> directory you should find an application with the same

Options

name as your original Python script.</p><p id="89c0">That is the desktop application, so go ahead an move it to your desktop and try it out by double-clicking it!</p><p id="15f0">Here is how it looks on my desktop:</p><figure id="35c3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*PF9bprMrKXIH44EvM_7TmA.png"><figcaption>The created desktop application. Source: own image.</figcaption></figure><p id="131f">You can of course rename it and you can also remove the created <code>dist</code> and <code>build</code> folders.</p><p id="30b9">If I double-click the application, the script is run, I know that because I can see the GUI-screen open up:</p><figure id="6acd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*q-5a6etHMHdS_-dGcDsiqQ.png"><figcaption>The GUI screen that is opened by the Python file that is run when I double-click the desktop application. Source: own image.</figcaption></figure><p id="b8e8">This was all about transforming a Python file into an easily startable application. We will use this method in our Making the Solitaire Game in Python series. You can find the first part of that series here:</p><div id="8073" class="link-block"> <a href="https://readmedium.com/making-the-solitaire-game-in-python-1-drawing-cards-from-a-shuffled-deck-de51c495dc"> <div> <div> <h2>Making the Solitaire game in Python #1 — Drawing cards from a shuffled deck</h2> <div><h3>In this series I will show you step-by-step how I made the card game Solitaire in Python. A great project to practice…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*dOagz_9NgX0u9MoxLq1PKg.jpeg)"></div> </div> </div> </a> </div><h1 id="c496">Thank you for reading!</h1><p id="fa9f">You can <b>get full access to all my posts by joining Medium</b>. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:</p><div id="ba8f" class="link-block"> <a href="https://medium.com/@BetterEverything/membership"> <div> <div> <h2>Join Medium with my referral link — Better Everything</h2> <div><h3>Read every story from Better Everything (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*aa4Y_6MHVoY6Wl-9)"></div> </div> </div> </a> </div></article></body>

Creating Desktop Apps from Python files in 5 Easy Steps

Have you ever created a Python file?

Then you probably know that you can run it from programs like IDLE, Visual Studio Code or command line. But when you are done building your script, wouldn’t it be easier if you could just run it by clicking an icon on your desktop?

Easily start your Python programs by clicking them on your desktop, after transforming your Python files into desktop applications. Image by catalyststuff on Freepik

In this short tutorial we will go over the steps to transform a Python file to an executable file. You can give the file an icon and place it on your computer’s desktop. Then, when you click it, the Python code will be executed.

This solution should work both on Windows and Mac computers.

Step-by-step

Step 1 — Opening Command Line

If you are on Windows open the Command Prompt. If you are on Mac open the app Terminal.

Step 2 — Installing the Python package pyinstaller

In the command line app that you just opened write: pip install pyinstaller and press enter. This will install the Python package pyinstaller, to learn more about Python packages and installing them with pip read:

Step 3 — Prepare the transformation from Python file to executable file

For our tutorial we have a directory (file folder) with a file called main.py with the following content:

import tkinter as tk

window = tk.Tk()

window.title('Dekstop App Example')

window.mainloop()

The above script opens a GUI (Graphical User Interface) by using the standard package: tkinter. This serves as a nice example because it gives us something visual: a GUI screen that opens on our screen.

In the same directory I put an icon. An icon is an image with the .ico file extension. The icon will be what we click to start the app.

The ico file that we will use as the icon of our dekstop app. Source: own image.

If you want to create your own icon you can read how I transformed a PNG image to an icon image here:

Step 4 — Use a pyinstaller command to transform a Python file into an executable file

Open the command line again and navigate to the directory where your script and icon are.

You can navigate to a certain directory with the cd command (cd for change directory).

For example: cd C:\Users\Better_Everything\Documents\Desktop App

In that directory run the following command, in which you have to replace the filenames of the icon and the Python file to the names you used:

pyinstaller -F -w -i good_app.ico main.py

  • pyinstaller is the package we installed in step 2 that transforms Python files into executable files
  • -F is to make sure our project gets combined into 1 file
  • -w is to make sure our program gets run without a command line screen opening
  • -i good_app.ico is to tell pyinstaller to use my image with filename good_app.ico as the icon for the executable file
  • main.py is the name of my Python file

Step 5 — Clean Up and Testing

After running the command in step 4, two directories should be created: dist and build. In the dist directory you should find an application with the same name as your original Python script.

That is the desktop application, so go ahead an move it to your desktop and try it out by double-clicking it!

Here is how it looks on my desktop:

The created desktop application. Source: own image.

You can of course rename it and you can also remove the created dist and build folders.

If I double-click the application, the script is run, I know that because I can see the GUI-screen open up:

The GUI screen that is opened by the Python file that is run when I double-click the desktop application. Source: own image.

This was all about transforming a Python file into an easily startable application. We will use this method in our Making the Solitaire Game in Python series. You can find the first part of that series here:

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

Python
Programming
Software Development
Automation
App Development
Recommended from ReadMedium