Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

Introduction to Tkinter

Meet Tkinter, Python's built-in GUI toolkit: what it is, how widgets, windows and the event loop fit together, how to check your install and when to choose it.

Tkinter is the graphical user interface (GUI) toolkit that ships with Python. With it you can open windows, add buttons, text boxes, lists and drawing areas, and react when the user clicks or types. The name is a wrapper around Tk, a small cross-platform GUI library that began life with the Tcl language; tkinter is Python's binding to it.

Because Tkinter is part of the standard library, there is nothing to download from PyPI. A program you write on Windows runs on macOS and Linux with the same code, and the result looks like a native desktop program. That makes it a good first GUI toolkit and a perfectly practical one for tools, utilities, teaching programs and small business apps.

About the examples. Tkinter needs a real display, and a web page cannot give a Python program one. Code that creates windows is therefore shown in read-only blocks: copy it into a file on your own computer and run it. Next to each GUI example, the runnable blocks on this site are stand-ins: plain Python that models the logic underneath (event loops, geometry maths, state) so you can experiment right here. They are labelled as such.

What a Tkinter program is made of

Every Tkinter program is built from the same four ideas.

  • A root window. One Tk() object is the main window. It owns the connection to the display.
  • Widgets. Labels, buttons, entries and the rest. Each widget has a parent (the window or frame that contains it), so the program is a tree.
  • A geometry manager. Creating a widget does not show it. You must hand it to pack, grid or place, which decide its position and size.
  • The event loop. mainloop() waits for things to happen (clicks, key presses, timers, redraws) and calls your functions.

The four parts of a Tkinter app

Root window One Tk() object
Widgets Label, Button, Entry...
Geometry manager pack, grid, place
Event loop mainloop() reacts

Build the widgets, place them, then start the loop.

1 / 3

The smallest program

Here is a complete Tkinter program. It opens a window with one line of text.

Python
import tkinter as tk

root = tk.Tk()
root.title("Hello")

label = tk.Label(root, text="Hello, Tkinter!")
label.pack()

root.mainloop()

Read it top to bottom. import tkinter as tk is the convention; the short alias keeps widget names readable (tk.Label). tk.Tk() creates the root window. tk.Label(root, ...) creates a label whose parent is root. label.pack() asks the packer to place it. root.mainloop() starts the event loop and does not return until the window is closed.

Everything after mainloop() runs only once the user closes the window. That is why GUI programs are usually organised as "build the interface, start the loop, and let callbacks do the work".

Checking that Tkinter is installed

On Windows and macOS, the installer from python.org includes Tkinter. On many Linux distributions it is a separate package (for Debian and Ubuntu: sudo apt install python3-tk). A quick way to test an installation from the command line is python -m tkinter, which opens a small demo window and prints the Tcl/Tk version.

You can also test from inside Python. The next block does it safely: if the module is missing (as it is in this web runner), the program reports that instead of crashing.

Python Is Tkinter available here?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not be surprised by the message: this page runs Python inside your browser, which has no desktop display to draw on. On your machine the first branch runs and prints something like 8.6.

Naming: tkinter, Tkinter and tk

Python 2 called the module Tkinter; Python 3 renamed it tkinter. Old tutorials that say from Tkinter import * are for Python 2. Today you write one of these:

Python
import tkinter as tk                 # recommended
from tkinter import ttk              # themed widgets (chapter 7)
from tkinter import messagebox       # dialogs (chapter 5)

Avoid from tkinter import *. It dumps about 150 names (Button, Label, END, LEFT...) into your program, which hides where names come from and can clash with your own variables. The tk. prefix costs three characters and pays for itself.

Classic widgets and ttk

Tkinter contains two families of widgets.

  1. Classic widgets (tk.Button, tk.Label, tk.Entry...): configured directly with options such as bg, fg, font.
  2. Themed widgets (ttk.Button, ttk.Label, ttk.Entry, plus new ones such as ttk.Combobox, ttk.Treeview, ttk.Notebook): drawn by the operating system's theme and styled through a Style object.

Early chapters use the classic widgets because their options are easy to see. Chapter 7 covers ttk in depth, and for modern-looking programs you will use a mixture.

A model you can run: the widget tree

Understanding that a Tkinter interface is a tree makes later topics (layout, destroying windows, focus order) click. The stand-in below builds a small tree and prints it with Tk-style path names, the strings Tkinter uses internally, such as .frame.button.

Python Stand-in: a widget tree with path names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that destroying form removed its label and entry too. The real widget.destroy() behaves the same way, and destroying the root closes the whole program.

Python Stand-in: the shape of every GUI program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second click:Save never ran because Quit ended the loop. The next lesson builds a real window; The Event Loop later makes this loop much more realistic.

When to choose Tkinter

Pick Tkinter when you want a desktop tool with zero dependencies, when you teach or learn GUI programming, or when a script needs a quick form instead of command-line prompts. Choose something else when you need a very polished consumer look, hardware-accelerated graphics (see Pygame or Qt) or a web-based interface. Within its range, Tkinter is stable, documented and installed almost everywhere.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does root.mainloop() do?

    mainloop() runs the event loop. It returns only when the main window is closed.
  2. 2. Which line makes a Label appear inside the window?

    Creating a widget only builds it. A geometry manager such as pack(), grid() or place() is what shows it.
  3. 3. Why is 'from tkinter import *' discouraged?

    Star imports pollute the namespace. import tkinter as tk keeps names clear.
  4. 4. What happens to a frame's child widgets when you destroy the frame?

    Widgets form a tree, so destroying a parent destroys all of its descendants.

Summary

  • Tkinter is Python's standard GUI toolkit, built on Tcl/Tk and available on Windows, macOS and Linux.
  • A program has a root window, widgets arranged in a parent/child tree, a geometry manager and the event loop.
  • import tkinter as tk is the recommended import; ttk provides themed widgets.
  • Creating a widget does not show it; you must call pack(), grid() or place().
  • GUI code needs a display, so on this site window code is read-only and logic is practised with labelled stand-ins.

Next: Your First Window