How to create a GUI in Python 3.7 – Beginner Friendly

Welcome back, everyone! Today we introduce a new category: Programming! I wanted to get started with Programming for a long time (read more about it at the end of this article), and now finally is the time. In this short Tutorial, I will show you how to create a GUI in Python using Tkinter.

There will be a whole series on Python, so this first article only covers how to create a GUI in Python and I explain to you what the different lines of code do.

Let’s get started with it.

Step 1 – Importing the Tkinter module

Open your favorite Python IDE (I use Atom). To use Tkinter, we need to import the module first.

#--------------
#Imports
#--------------
from Tkinter import *

What this does is, it imports the Tkinter and everything that belongs to the Tkinter class, so we don’t need to import any further Tkinter modules later on.

Step 2 – Creating a Blank Window

Now that we have imported Tkinter, we can create a Blank Window that will serve as a frame, or background for our GUI. Therefore, we are creating a new variable called “mainWindow” and assing it the Tkinter class.

Inside the parenthesis of the Label object, we have to tell our program that we want to put the Label on our mainWindow variable. Also, we need to tell our program to use the text parameter and assign a text to it.

Step 4 – Assigning the Location for the Label

Now we still need to tell the programm where to place this Label, or Text, on our new shiny new GUI Window. To keep things easy, we use the pack extension for this. What pack does is, it packs (or puts) the text literally wherever it finds the space for it. We will talk about more specific placement options later on, in another tutorial.

Step 5 – Assigning the Location for the Label

The last thing we need to do is to assign mainloop to our mainWindow variable. Whenever you have a GUI, you want that Window to stay open on your screen until you click on the little Red X on the corner to close it. To achieve this, we need to make use of a loop, the so-called mainloop. This loop keeps running until it breaks by your interaction of pressing the close button (or the Red X).

If you have followed along every step and you run your code, you should see a empty window popping up, which you can close by breaking the code, or, hitting the Red X Button in the upper right corner. Therefore, you just learned how to create a GUI in Python in a few steps.

In the next tutorials, we will look at how to create buttons, how to change button placement and how to assign functions to those buttons.

Conclusion

Learning to code, or specifically learning Python is on a long list of things that I want to learn. I dabble in Programming for a long time now but never really had the time (or the project) to really stick with it. Back in the day, I started Ceos3c out of the sole purpose of documenting stuff for myself. Turns out, documenting stuff really helps you to actually learn it. Now I want to do the same with Python. I am an absolute beginner, so I will post short bits of the stuff that I learned while trying to get a grasp of Programming.

Keep in mind that those are tutorials written from a beginner, to other beginners. Happily leave a comment and point out any errors 🙂