Creating your first desktop app with Python
Php 23-May-2017

Creating your first desktop app with Python

Python enables developers to craft desktop applications. In this tutorial, we are going to create a desktop app in Python and make an executable for Windows. The application is going to be simple – it would allow users to select an article from a list, click read and the article will open in their browser. We are going to use Tkinter to craft the GUI that our application needs.
Our application imports the following modules:

from Tkinter import *
import webbrowser
import os
import data

Data is our own module (file) which contains a list with dictionaries which define titles of articles and URLs where they can be found.

The data module looks like that:
articleList = [
{'name': "An overview of Laravel 5 Facades through the Session and Auth facades",
'url': ""
{'name': "Creating a Simple Plagiarism Checker in Node.js",
"url": ""},

Webbrowser allows to open the user’s browser to different websites and the os module contains utility methods such as a method that allows us to join paths in a OS-independent way or get the OS name.
class Reader(Frame):

def __init__(self, master=None):
self.articleList = data.articleList
self.readingInformation = None

We create a class called Reader which accepts the Tkinter’ sframe and create the __init__ method which works like PHP’s __constructand it allows us to run logic whenever our class is instantiated.

In it, we set some properties that the class will have. articleList would contain the data that we want to show in the app and readingInformation would contain a message that is shown to users when they open up an article. Afterwards, we call the createWidgets method on our class.

The createWidgets method loops over the list with dictionaries and creates a new list which contains only the title (name) of each article. Then, it creates a variable called optionSelected which will hold the title of the article that is currently selected by the user and initializes it to the first article in the list.

Afterwards, an OptionMenu UI is created and we pass it the variable from which we would retrieve selected options and we also pass it the unpacked list of articles which would be added as options. We set the OptionMenu to have a green background and pack it. Then, we create a Button UI, set its text, foreground (text color), background and provide a callback which will be executed when the button is clicked with the help of the command key. In our case, the readArticle method of the Reader class will be executed. Finally,  we create a Label UI which will just be some text at the bottom of the window. The second parameter called  textvariable would allow us to change the text of the label anywhere in our application logic.  Then, we set the font family to Arial and the font size to 16 with the help of the font key. Finally, we set the foreground to a hexadecimal color and pack the label.

def createWidgets(self):
articleNamesOnly = []
for x in self.articleList:
self.optionSelected = StringVar(self.master)
self.optionSelected.set(articleNamesOnly[0])  # initial value
option = OptionMenu(self.master, self.optionSelected, *articleNamesOnly)
option['bg'] = "green"
readBtn = Button(self)
readBtn["text"] = "Read"
readBtn["fg"] = "white"
readBtn['bg'] = "crimson"
readBtn["command"] = self.readArticle
self.readingInformation = StringVar()
readingLabel = Label(self.master, textvariable=self.readingInformation)
readingLabel['font'] = ("Arial", "16")
readingLabel['fg'] = "#666"

The readArticle method which is executed when the read buton gets clicked just finds the URL of the article that is selected (remember that we have the title of the article saved in our optionSelected property), opens it in the user’s web browser and updates the Label UI’s text.
def readArticle(self):
url = ""
for x in self.articleList:
if x['name'] == self.optionSelected.get():
url = x['url']
self.readingInformation.set("Reading " + self.optionSelected.get() + "!")

Finally, we create our Tkinter app, set the title of the application’s window, add an icon to the application’s window (an image file in the same folder where the current .py file is in)

root = Tk()
root.wm_title("Web Development Articles Reader |")
if "nt" ==
root.wm_iconbitmap(bitmap = "favicon.ico")
root.wm_iconbitmap(bitmap = "@favicon.xbm")
app = Reader(master=root)


Compling the Python application into an executable for Windows

To make the executable we are going to use pyinstaller. If you have installed Python properly, you just need to run your Terminal and type pip install pyinstaller to get it. Otherwise, you would have to set the path to the Python folder and the path to the Python Scripts folder in your PATH environment variable (something like C:/Python27 and C:/Python27/Scripts for Python 2.7).

Now, you can just browse to the folder where your app is and run pyinstaller.exe–onefile –icon=favicon.ico to create a single executable file (.exe) from your application. In our case, after it is created we would need to add the two icon files to the directory where the executable is – otherwise they would not be available and the application will crash.

Our application files

Figure 1: Our application files

And, voila, we have made our first desktop application in Python!

The finished articles reader application in Python

Figure 2: The finished  articles reader application in Python