There are numerous Graphical User Interface (GUI) modules that work with Python. Tkinter (Tk interface) is the standard python GUI module.
Let’s start with the basics – what is a GUI and why do you need one? Well, if you’ve ever used a Windows, Mac, or Linux computer (spoiler: if you’re here, you’re using one), then you’ve experienced a GUI. A GUI is simply the front end of a computer program. It provides the user an interface that is beyond a simple text terminal application. If you want your program to have any chance of success in the real world, you’ll probably need a GUI for it. So, let’s learn how to use Tkinter to start building an interface for our Python programs.
Step 1: The first step to using Tkinter to make an interface is to import the module. This will give you access to the complete Tkinter tool set to build your application. Only use one of the below import methods. Note, if you use the first method you must prefix any tkinter method of function with “tk.”; if you use the second method you don’t need to do this.
# My preferred method, import tkinter so you can reference it as tk import tkinter as tk # An alternative you'll often see tells python to import all modules from tkinter from tkinter import *
Step 2: Create a “root” object (main window) for the interface. This is the primary application window that will be used. You’ll build your application into the root object. Only one root object should ever be created for a program.
root = tk.Tk()
Step 3: Create a label object for the window. Label objects can contain images or text for the window. The first two lines setup variables to store that information. The second two lines setup the window objects. The first parameter in the window objects tells tkinter which window the label belongs to, the second is a keyword telling it what the label actually is (image or text).
world_pic = tk.PhotoImage(file = "happy-world.png") label_text = "Hello, World!" window_obj1 = tk.Label(root, text = label_text) window_obj2 = tk.Label(root, image = world_pic)
Step 4: We can now “pack” these objects into the window root we created in step 2 and make a program loop. The pack method allows us to specify where in the window our objects are displayed. The mainloop tells tkinter to keep the window active until the user closes it (runs the event loop). This blocks the rest of the program from processing until the window is closed by the user.
window_obj1.pack(side = "left") window_obj2.pack(side = "right") root.mainloop()
The resulting window looks like this:
The complete program code is below:
import tkinter as tk root = tk.Tk() world_pic = tk.PhotoImage(file = "happy-world.png") label_text = "Hello, World!" window_obj1 = tk.Label(root, text = label_text) window_obj2 = tk.Label(root, image = world_pic) window_obj1.pack(side = "left") window_obj2.pack(side = "right") root.mainloop()
We’ve complicated the code a little bit for clarity in this tutorial. We can simplify by combining lines like this, and we get the same result!
import tkinter as tk root = tk.Tk() world_pic = tk.PhotoImage(file = "happy-world.png") window_obj1 = tk.Label(root, text = "Hello, World!").pack(side = "left") window_obj2 = tk.Label(root, image = world_pic).pack(side = "right") root.mainloop()
I hope you enjoyed and learned from this post about getting started with tkinter and python. We saw how to make a basic window object and display both text and images with tkinter.
Keep and eye out for my next post in this series which will cover input boxes and clickable buttons.
Let me know if you have questions or improvements in the comments below!