Python GUI App In No Time by Lazar Gugleta | Towards Data Science

One of the most important parts of today's applications is GUI (Graphical User Interface) itself.
That is because the market is so competitive and oversaturated that it is not even about the functionality anymore, but who can make it look prettier and simpler to use.
Today we will be learning how to create and build GUI applications in one of the most popular programming languages Python.
This short tutorial will be explained line by line and will be very easy to follow although you might not have much experience with Python or programming in general.
For the development of this app, you will not need much, If you are already familiar with Python, you can skip until the next chapter of this article.
My personal choice for code and software development is Visual Studio Code.
Next up we will need Python installed and so if you are on the Ubuntu or similar Linux distributions, Python comes pre-installed.
If that is not the case, you can install it like this:
sudo apt-get install software-properties-commonsudo add-apt-repository ppa:deadsnakes/ppasudo apt-get updatesudo apt-get install python3.8
*at the time of writing this article the Python 3.8 is the latest stable version and deadsnakes is recommended PPA to use.
On other operating systems like windows or mac, you are going to install it by downloading the executable installation and simply running it.
We also have to install the Tkinter library for development:
sudo apt-get install python3-tk
The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit.
At the beginning of our coding with importing the required libraries:
import tkinter
Once we imported them, we are able to use them in our code and usually, your code editor will be able to recognize keywords from those libraries and make typing easier for you as much as possible.
Next up is creating the window, that we will be using to add elements to and have as the main window of our GUI app.
window = tkinter.Tk()
Top-level widget of Tk which represents mostly the main window of an application. It has an associated Tcl interpreter.
After you initialized the window all you have to do is call the main loop of Tk and it will execute all the commands you set for GUI.
window.mainloop()
Mainloop takes care of any events, like button clicks or text box entries that come after it from running until the window is closed.
Run your Python file using the command:
python3 GUI.py
Here is our first window!
This is just the beginning of our app and what we have to do next is customize our window!
Adding a title to our window is very simple:
window.title("Python GUI")
An important thing to note here is that every single command that we want to be executed for our window has to be before “window.mainloop()” because otherwise it won’t be shown in GUI.
Setting a specific window size is rather important because you might want more space for your elements that we are going to add later on!
Way to do it:
window.geometry("450x250")
The first part is width followed by separation as “x” and height number.
After setting basic window settings, we are ready to add elements to our GUI app.
There are different kinds of elements we could use here and that are provided to us by the Tkinter library, but we will focus on the four most essential to every GUI application.Those are: Label, Button, Entry(Input), and Text
In every GUI it is essential to create some sort of text display to the user that is using the program.
label = tkinter.Label(window, text="Some label text",width = 50)
We save our label under the variable named ‘variable’ and define it that it belongs to our main ‘window’, contains some text and it is possible to define extra parameters like width, etc.
An important step after defining the label we have to pack it and set it into the window. There is a function called pack() for exactly that:
label.pack()
This geometry manager organizes widgets in blocks before placing them in the parent widget.
We use buttons to execute some type of command or a backend functionality that our app might require.
Since we need some kind of function, we define ‘clicked’ to create a new label every time the button is clicked.
def clicked():  label = tkinter.Label(window, text="Button label",width = 50)  label.pack()butt = tkinter.Button(window, text="Click button!", command=clicked,width = 10)butt.pack()
Every time you click the button, it will create a new object with a type Label, which will be displayed under the previous one.
This element is called Entry in Tkinkter library, but is also familiar under the term Text Box for many other programming languages.
Let’s see how we can implement it:
entry = tkinter.Entry(window, width=10)entry.pack()
Useful function of the Entry element is that we can retrieve what the user has written in it, but more about that in the example of the binary convertor.
Specialized Text Box, which is much more customizable than the regular Entry.
And here is how we use it:
text = tkinter.Text(window, width=20, height=5)text.insert(tkinter.INSERT, "Hello")text.insert(tkinter.END, "World")text.pack()
We have parameters like INSERT and END so we can specify the position of the text we want. For more information go here.
Once we are done with covering all the essential elements, let’s build an actually app with what we just learned.
We should create something that would make sense and is useful to us.
My first though is simple calculator so we can utilize all elements we were talking about during the tutorial.
Let’s take a look at the complete code I have written here:
We basically use all elements that we talked about and I crated simple Text and Entry manipulations inside the add and sub functions.
Every time you click to add two numbers, the Text element gets cleared and new value is inserted.There is also ValueError handling, which will catch anything else but numbers, which are fine.
Here is the complete code for both tutorial and calculator example.
github.com
That is all for today my beautiful people.
This is just a really simple GUI in Python, that in my opinion everyone is able to do If they follow along!
Although simple it is a very powerful software that could be used to create real world apps!
Stay creative and follow me for more tutorials like this one!