Your first window
Last updated
This module is optional, and it stands on its own: you can do it, the Flet module, both or neither. It puts a desktop window in front of the library system from the capstone. You'll start with tkinter, the window toolkit that comes with Python, and in this lesson you'll open your first window and see what keeps it open.
By the end of this lesson you can
- Open a window with
tkinterand put widgets in it - Explain what the event loop does, and why a window program waits instead of running top to bottom
- Say what happens when
mainloop()is missing
Programs that wait
The capstone's menu (lesson C.7) asked one question at a time: it printed >, waited for a line, did the command, and asked again. The program chose what happened next.
A window can't work like that. The user might click any button, type in any box, or close the window, in any order. So a window program turns things around: it sets up the window, then hands control to an event loop. The loop waits for the next event (a click, a key, a close) and calls the code that belongs to it. Your code runs only when something happens.
Here's a pretend event loop in plain Python, with a list of events standing in for the user. Press Run:
In tkinter the loop is one method call, root.mainloop(). It runs until the window closes. Only then does the line after it run.
Check that tkinter works
Windows can't open in this page, so this module runs on your computer, in the project folder from the capstone. Open a terminal there, and turn on the virtual environment as lesson 2.9 showed.
tkinter comes with Python on Windows and macOS. Check it with its own test window:
python -m tkinter
A small window should open, with a button or two. Close it. If Python says No module named 'tkinter' instead, you're probably on Linux, where tkinter is a separate system package. On Debian and Ubuntu it's this (other systems name it in their own package lists):
sudo apt install python3-tk
tkinter drives Tk, a toolkit for windows that's older than Python itself. Both are free to use, and their licences let any government or business use them.
Your first window
Save this as first.py, in a new folder or anywhere outside library/:
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Library desk") welcome = ttk.Label(root, text="Welcome to the library") welcome.pack(padx=40, pady=(30, 10)) close = ttk.Button(root, text="Close", command=root.destroy) close.pack(pady=(0, 30)) root.mainloop() print("The window has closed.")
Run it:
python first.py The window has closed.
first.py on Windows 11. tkinter follows each system’s look, so it’s a little different on macOS and Linux.The window opens, and the terminal waits. Press Close: the window closes, mainloop() returns, and only then does the last line print.
Line by line:
tk.Tk()makes the main window. There's one per program, and it's usually calledroot.ttk.Label(root, text=...)makes a widget. Every widget is an object, and the first argument is its parent: the window or frame it sits inside. So widgets form a tree, withrootat the top.- Making a widget doesn't show it.
.pack()places it in its parent, one under another.padxandpadyadd space around it, in pixels; a pair like(30, 10)means 30 above and 10 below. command=root.destroytells the button which method to call when it's clicked.destroycloses the window.
ttk is tkinter's set of themed widgets, which follow the look of your system. tkinter has older plain widgets with the same names, like tk.Label; this module uses ttk wherever it can.
Try a change: add a second label under the first, saying how many items the library holds.
Leave out the loop
The wrong way. Here's the same program without root.mainloop():
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Library desk") welcome = ttk.Label(root, text="Welcome to the library") welcome.pack(padx=40, pady=(30, 10)) close = ttk.Button(root, text="Close", command=root.destroy) close.pack(pady=(0, 30)) print("The end of the script.")
python first.py The end of the script.
No error, and no window either, or one that flashes and vanishes. Python made the window, packed the widgets, reached the end of the script and stopped. When a Python program ends, its windows go with it. Nothing was waiting for events, so nothing kept it open.
So every tkinter program ends with mainloop(). (Some editors, like IDLE, keep their own loop running, so a window can seem to work there without it. Run your programs from a terminal to be sure.)
Check
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- The Python Standard Library: tkinter
- tkinter: A Hello World Program
- The Python Standard Library: tkinter.ttk: the themed widgets: Label, Button, Entry and the rest