In this Tutorial, I will show you how to create a GUI in Python using Tkinter. We take it from the very beginning in a beginner-friendly fashion.
Table of Contents
- Step 1 – Installing Tkinter
- Step 2 – Importing the Tkinter module
- Step 3 – Creating a Blank Window
- Step 4 – Giving our GUI Window a Name
- Step 5 – Assigning the Location for the Label
- Step 6 – Assigning the Location for the Label
- Conclusion
Step 1 – Installing Tkinter
In this example, we are using Ubuntu to run our code. Here, we first need to install the python3-tk
module:
sudo apt update && sudo apt install python3-tk
Code language: Bash (bash)
Step 2 – Importing the Tkinter module
Open your favorite Python IDE (I use Visual Studio Code). To use Tkinter, we need to import the module first:
#--------------
#Imports
#--------------
from Tkinter import *
Code language: Python (python)
What this does is it imports the Tkinter module and everything that belongs to the Tkinter class, so we don’t need to import any further Tkinter modules later on.
Step 3 – 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 assigning it to the Tkinter class (Tk()
):
#--------------
#Imports
#--------------
from Tkinter import *
#--------------
# Main Window
#--------------
mainWindow = Tk()
Code language: Python (python)
Step 4 – Giving our GUI Window a Name
Of course, we want that our GUI has a name. For this, Tkinter uses Labels. We are going to assign the label object
to our mainWindow
by creating a new variable called mainWindowTitle
.
#--------------
#Imports
#--------------
from Tkinter import *
#--------------
# Main Window
#--------------
mainWindow = Tk()
mainWindowTitle = Label(mainWindow, text="Our New GUI")
Code language: Python (python)
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 5 – Assigning the Location for the Label
Now we still need to tell the program where to place this Label, or Text, on our 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:
#--------------
#Imports
#--------------
from Tkinter import *
#--------------
# Main Window
#--------------
mainWindow = Tk()
mainWindowTitle = Label(mainWindow, text="Our New GUI")
mainWindowTitle.pack()
Code language: Python (python)
Step 6 – 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):
#--------------
#Imports
#--------------
from Tkinter import *
#--------------
# Main Window
#--------------
mainWindow = Tk()
mainWindowTitle = Label(mainWindow, text="Our New GUI")
mainWindowTitle.pack()
mainWindow.mainloop()
Code language: PHP (php)
Now we can run our code by typing:
python3 test1.py
Code language: Bash (bash)
If you have followed along every step and you run your code, you should see an 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.
Conclusion
Learning to code, or more specifically, learning Python, is on a long list of things I want to learn. I dabble in Programming for a long time now but never really had the time (or the project) to stick with it. Back in the day, I started Ceos3c out to solely document stuff for myself. It turns out that documenting stuff helps you actually to 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.
🐍 Learn Python Programming
🔨 Python Basics
👉 Python Syntax
👉 Python Variables
👉 Python print()
👉 Python input()
👉 Python Constants
👉 Python Comments
⚙️ Python Specifics
👉 Filter Lists in Python
👉 Replacing List Items in Python
👉 Create a GUI in Python
👉 Find out the Length of a String in Python
👉 Enum in Python
👉 Python Inline If/Else One-Line Statements
👉 Python xrange()
👉 Python List Slicing
🏋️ Python Exercises
👉 Python Exercise Collection
try:
from Tkinter import *
except ImportError:
from tkinter import *
mainWindow = Tk()
mainWindowTitle = Label(mainWindow, text=”Our New GUI”)
mainWindowTitle.pack()
mainWindow.mainloop()
This code runs on python 3.7.3 but does not produce a “window”. After 2 years I’m still looking up code examples and finding everything else beside what works. I doubt seriously that Python is a the software of choice for young programmers. It is a bag of worms when it comes to documentation.
The guru that cleans up Python and thoroughly documents it could be wealthy.
Please try this
import tkinter as tk
window = tk.Tk()
window.mainloop()
I just want to point out that Tk doesn’t exist anywhere in your code, do what the other reply sais.
I just want to point out that to import Tkinter, the import statement must have Tkinter all lowercase.
Do you have any more references for create buttons, how to change button placement and how to assign functions to those buttons?
Nice!