What exactly is a Python GUI used for?
Graphical User Interfaces provide an easier way for users to interact with programs. There are quite a number of frameworks in Python that can be used in developing GUIs.
However, Tkinter is the de facto package when it comes to developing user interfaces in Python.
How to make a Python GUI?
Since Tkinter is built into the Python standard library we just need to import it at the top of our code. Once that is done we can now access the various functions and classes within the Tkinter package.
import tkinter as tk
We can now go ahead and create a window which is basically a container of Tkinter within which all widgets will be built.
Now since a window is an instance of the class Tk() that is within the Tkinter package, we can go ahead and initialize the new window with a variable window as shown below.
Now that we have created a window we can go ahead and create new widgets that will reside within this new window.
There are different types of elements also called widgets that provide controls for users, some of them include: buttons, text boxes, text and canvas.
Creating Widgets
In this article, we will explore three main widgets including a button, labels and entry. Using the tk.Label() class we can now add a new widget with the text ‘Enter your name’ and assign it to a variable ‘label’.
Furthermore, we also need to add the window.mainloop() method that tells Python to run the Tkinter event loop.
This method allows the windows that we have created to be responsive to events that come after it until the window that the method runs on is closed.
It is evident that although we did create a label in the code above the text didn’t actually show on the window at all. This is because we have not added the widget to the window yet.
Although there are several ways of adding widgets to the window in this case we are going to use the .pack() method provided by the Label widget.
Notice that the GUI window is very small, this is because Tinker resizes the window to be as small as possible while maintaining the actual size of the widgets within that window.
Now, we can work our way around this by making use of frames that create a rectangular region around a group of related widgets and furthermore provide padding between widgets.
However, we can also control the height and width of the label by specifying the height and width parameters within the label as shown below.
Note that this is not the only aspect of the widgets that we can change, we can customize the default system colour of the text as well as the background colour of the window.
Creating User Entry Widgets
We are also going to create a small entry box underneath and a button and another label. So when a user clicks on the button, it will take whatever that the user has entered in the text box and return a Hello alongside that text entered by the user.
There are three main methods that we can use with entry widgets, this includes Inserting text, Retrieving text from the user or deleting text.
Since we have already imported Tkinter, and a new window is in place, furthermore we have already created a label that lets the user know the kind of text to input.
We can go ahead and create an Entry widget. We can create an Entry widget by initializing tk.Entry() to a variable name ‘entry’. This widget is also going to have a width of 25.
Creating a Button Widget
To create a button, we use the tk.Button() that takes in several arguments such as the ones shown below.
button = tk.Button(container, text, command)
We can also specify the width of the button and add some text that will be displayed alongside the button.
Now, in a similar manner Entry widget and Button widgets as well as the Label widget also need to have the .pack() method in order to be displayed in the window that we initially created.
Creating a new Label widget and Function
Just underneath the Button widget, we are going to create a new Label widget that is going to display text from a function that we will create at the top of our code.
Now, this label is much like the one that we created earlier on, except that in this case, we will initialize with a different variable named ‘out’.
Label widgets allow us to create text that will be displayed to the user and cannot be edited by the user. This label will also have the parameter of height and width is equal to the label widget that we created earlier on.
Now at the top of our code, we are going to create a function that will take in text from the Entry widget. Using the .get() method we can retrieve the text entered into the Entry widget and assign it to a variable, in this case, we have assigned it to a variable named ‘value’.
F string is a new Python syntax that allows us to include a value of an expression and display it alongside a string.
In this case, we’re using the f string to display the text entered by the user alongside the word Hello, we have then assigned this to the Label widget so that it is now displayed in the window.
Now when we execute the function above, the text field in the out label will be initialized to the f string that we have created that in turn picks up the text Entry widget and formats it to appear together with the word ‘Hello’.
At this point, if we run the code above we will certainly get a window with an Entry box and button. However, once we enter a text and press the button, nothing is likely to happen.
This is because we have not yet assigned the function ‘Hello()’ to the button widget. To assign the function to the button widget simply add command=hello() as a parameter to the Button widget.
Summary
This is how to make a Python GUI simple and easy. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
