How to make a Python GUI? – with example

  • Reading time:20 mins read

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.


how to make a python gui

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.


how to make a python gui


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.  



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

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!





Not sure if programming is for you? With CodeBerry you’ll like it.