You look at windows everyday on your computer or phone, click on links or type in boxes. But have you ever wanted to try and make your own? Today, let's take a look at how to get started making our own window, or GUI, using Tkinter and Python.
Rough idea of what a GUI could look like.
Example Desktop GUI
What Is A GUI?
Every time you interact with the screen on your computer or your phone you are more than liking using a graphical user interface, or GUI (pronounced gooey). Those dazzling rectangular windows with buttons, icons, and menus were created for us to have a much easier way to interact with our electronic devices. Older methods, such as MS-DOS, used the command line and text.
While Python has a number of toolkits for making your own GUIs, Tkinter is good for learning the basics about UI development. For these tutorials, we'll focus on making our own GUIs using Python and the Tkinter module. We'll begin by going over some of the basics -- creating an interface's window and learning how to display images and text -- so that you can work towards thinking of ideas for how you can create your own applications.
What Is Tkinter?
Tkinter is Python's standard GUI package. It is an object-oriented layer on top of the open-source Tcl/Tk widget toolkit. While there are more feature complete packages available for creating a GUI, such as PyQt, Tkinter remains popular as it is simple, widely used by beginners and has a ton of references to get you started.
Creating A Window
Let's start with the most basic idea: creating a window. It's really quite simple using Tkinter. Let's create a new file called basic_window.py
.
Never miss an update
Enjoyed this? Subscribe to get new updates straight in your Inbox.
from tkinter import *
root = Tk()
root.mainloop()
And that's it. Not very interesting... yet.
A small, blank empty window.
The first line imports all of the functions for the module Tkinter. While it is usually bad practice to use import *
, for right now let's just use it to simplify things. root
in line 2 tends to be the standard name for initializing the Tk root widget (that, or master
). The root
widget, which is the main window and our parent window, has to be created before any other windows. Also, there can be only one root. Finally, the last line runs the main event loop. Next, let's take a look at how to make some adjustments to our root window.
from tkinter import *
root = Tk() # create a root widget
root.title("Tk Example")
root.configure(background="yellow")
root.minsize(200, 200) # width, height
root.maxsize(500, 500)
root.geometry("300x300+50+50") # width x height + x + y
root.mainloop()
Let's take a look at what's new. In lines 4-8, we are able to call on methods related to the root window. In line 4, we can change the title of the window. You can also change the background color of the window, shown in line 5.
You can also adjust the size of a window. We set the minimum size (line 6) and maximum size (line 9) for the window. Finally, let's talk about geometry()
. Simply put, geometry allows us to consider how the window will look when we first run the program. We can set the starting size of the window as well as where it will first appear on the screen by specifying the width, height, x, and y coordinates. If the x and y coordinates aren't set, then the window will show up in the top-left corner, (0,0), of the screen.
Creating A Label
In Tkinter, you can use widgets to give your GUIs various properties and functionalities. Labels are the most basic widget and can display either text or images. Open a new file called create_label.py
.
from tkinter import *
root = Tk()
root.title("Tk Example")
root.minsize(200, 200) # width, height
root.geometry("300x300+50+50")
# Create Label in our window
text = Label(root, text="Nothing will work unless you do.")
text.pack()
text2 = Label(root, text="- Maya Angelou")
text2.pack()
root.mainloop()
Let's take a look at the above code. First, we import the Tkinter module and then set up the main window and it's parameters. In line 9, we create a Label
widget for displaying text. The first parameter is our root
window, followed by the text that we want to display. The pack()
method that is called on text
in the next line is used to fit the size of the window to the text (it "packs" a widget in the parent window). Also, if we adjust the window size the label will stay in the top-center part of the window. There are other ways to position a label or other widgets in different areas, including place()
and grid()
.
There are a few ways to load images in Tkinter. For this example, we'll see how to do it with a Label
widget.
from tkinter import *
root = Tk()
root.title("Tk Example")
root.minsize(200, 200) # width, height
root.geometry("300x300+50+50")
# Create Label in our window
image = PhotoImage(file="025.gif")
img = Label(root, image=image)
img.pack()
root.mainloop()
In line 9, we make a call to the PhotoImage
class to load the image file we want to use. You'll can pass your own image file to PhotoImage
. The Label
widget is called, but the text
parameter is changed to the image
we want to display. The PhotoImage
class can only load GIF and PGM/PPG image formats. There are other ways around this using Pillow.
Window complete with text and an image.
What is important to notice here is the way that the pack()
method displays the text and images. Each Label
is placed in the parent window in an optimized way. We can also specify the relative positions of the widgets using pack()
. Experiment by adding your own images or text and see how it changes the window.
Summary
In this tutorial, we covered how to create the parent window and add text or images using the Label
widget in Tkinter. Next time, we will consider the layout of our GUI and learn about the three methods we can use.
It is important to apply the things that you learn. Take some time to experiment with the window's geometry or the images and text that can be displayed on a labels.
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PySide6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!