If you are not a software engineer, you may work with people that don’t know any programming. However, your Python solutions can increase the team’s efficiency and reduce workload tenfold. But what if you need to teach others how to execute it too? Using the terminal or other user interfaces may look too foreign and complicated for many.
A graphical interface is a crucial part of any application, as it is the way through which a user sees and interacts with the application. You might have a very useful, performant, and efficient application, but all might go in vain if you don’t have a good GUI, or worst not have at all.
Not everybody can work with the command line and not everybody is highly skilled with software development, and therefore GUI is the standard way through which people interact with any application. Moreover for no developer folks using a command line can be a nightmare.
Python is one of the most used languages due to its easy learning curve and versatility, as it can be used for a large number of things. And to keep its promise, Python does not shy away from GUI as well.
Python has a lot of libraries that can be used to build GUI applications, which are not only fast but quite robust. Many of the libraries can be used for mobile application development.
Here are some of the most popular and used libraries that are used for GUI development
- PySide 2
In this blog we will focus on creating a GUI using the Tkinter library. There are two reasons to choose the Tkinter library, one for its large developers community, popularity and other for its ease of use.
Building a GUI with tkinter is potentially the easiest when compared with the other libraries in the market. The other libraries might be more powerful, cross platform. But Tkinter beats them all when it comes to simplicity and ease to build a GUI.
Therefore, let’s get straight into the building part. Here is a simple script, you can see for yourself, how easy it is to build a graphical user interface.
Let’s discuss the code in detail, and look at what is actually going on.
1. As you can see, we first need to import tkinter, since tkinter is a built in python library you don’t need to download the module from the internet or using pip install.
2. Just below, on the next line, we create a window, in which all of our components will be displayed.
3. In the next line, we create a label widgets to be displayed in our window which we have created.
4. And finally we run the window main loop.
How Tkinter functions:
The working of the library can be broken into four simple parts.
- Import tkinter module
- Create a main GUI window
- Create widgets
- Run the loop
Now the widgets are basically small components that we like our GUI main window to have, for eg button, slide bar, images. All of these components are embedded into our single main window using widgets.
The main loop is basically a forever loop that keeps running unless we close the window, this loop keeps displaying the widgets unless we close the window manually.
To abstract things, you can think of widgets as lego blocks. We put a lot of widgets together to build something that is useful, just like we put together lego blocks to build a car.
There are several widgets at your disposal while using the Tkinter library, I have added a few to give a brief knowledge of what tools you have at your disposal.
- Label: Used to display text or image on the screen
- Button: Add button to the window
- Canvas: Used to draw pictures and layouts
- ComboBox: This contains a down arrow to select from list of available options
- CheckButton: It displays a number of options to the user as toggle buttons, from which the user can select
- Entry: It is used to input single line text entry from user
- Scale: It is used to provide a graphical slider which allows to select any value from that scale
- SpinBox: This provides a set of values, from which the user can select
- Text: It allows user to edit multiline text and format the way it has to be displayed
- Message: It works same as that of label and refers to multi-line and non editable text
These widgets are the reason that Tkinter is so popular. It makes it really easy to understand and use practically.
These are some of the most used widgets, there are also a few more which you can refer to from the documentation of tkinter.
It is very important to realise the fact that you just can place the widgets into the main window, you need some geometry management, or in simple terms need to define the size, position where these widgets will be placed.
To manage geometry there are three functions:
- pack(): It covers up the entire space that is available, be it horizontally or vertically. This is the most popular way to display the widget.
- grid(): This creates a grid or table where in each cell, you can have a widget.
- place(): This function takes in the exact location of where you want to place that widget.
Create a button that displays a message box
Create login form
In this post, we had a brief look at some of the most popular libraries that can be used to build GUI in Python. And then learned how tkinter libraries work, its main widgets and functionalities. And lastly build some very basic GUIs.
You can experiment with the Tkinter library and build some of the coolest apps that you might have dreamt to build, all thanks to Python and Tkinter.
With this comes the end of the article, thanks for sticking till the end.