Skip to content

tkinter

The Python tkinter package provides a powerful object-oriented interface to the Tcl/Tk GUI toolkit. It’s the standard for creating desktop applications in Python, allowing developers to build cross-platform graphical user interfaces with ease.

Here’s a quick app:

Language: Python
>>> import tkinter as tk
>>> root = tk.Tk()
>>> label = tk.Label(root, text="Hello, Tkinter!")
>>> label.pack()
>>> root.mainloop()

Key Features

  • Provides a wide range of widgets, including buttons, labels, and text entries
  • Supports event-driven programming through callback functions
  • Enables layout management with geometry managers like .pack(), .grid(), and .place()
  • Offers access to the full power of the underlying Tk toolkit
  • Provides the modern themed widget set through tkinter.ttk, whose widgets adopt the platform’s native look and add extras such as Combobox, Notebook, Progressbar, and Treeview
  • Enables rapid development with built-in availability in the standard library
  • Allows cross-platform deployment on Windows, macOS, and Linux

Frequently Used Classes and Functions

Object Type Description
tkinter.Tk Class Represents the main application window
tkinter.Label Class Displays text or images
tkinter.Button Class Creates a clickable button widget
tkinter.Entry Class Creates a single-line text entry field
tkinter.Tk.mainloop() Method Starts the Tk event loop
tkinter.Tk.title() Method Sets the window title
tkinter.IntVar Class Manages an integer value for use in widgets and callbacks

Examples

Creating a minimal window with a label:

Language: Python
>>> import tkinter as tk

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

>>> root.mainloop()

That .pack() call is one of three geometry managers that tkinter gives you for arranging widgets. Switch between them below to see where each one puts the same label, entry, and button, along with the calls that produce each arrangement:

Interactive diagram — enable JavaScript to view.

Adding a button with a callback function:

Language: Python
>>> import tkinter as tk

>>> def on_button_click():
...     print("Button clicked!")
...

>>> root = tk.Tk()
>>> root.title("Button App")
>>> label = tk.Label(root, text="Click the button below:")
>>> label.pack()
>>> button = tk.Button(root, text="Click Me", command=on_button_click)
>>> button.pack()

>>> root.mainloop()

Common Use Cases

  • Developing desktop applications with a graphical user interface
  • Creating custom dialogs and forms for user input
  • Building educational tools and simple games
  • Prototyping interfaces quickly and interactively
  • Creating simple internal tools and automation dashboards
  • Embedding GUI elements in scripts or utilities

Real-World Example

Here’s an example of a counter application using tkinter. It demonstrates how to use tk.IntVar() to manage dynamic values that update the user interface in response to user interactions:

Language: Python
>>> import tkinter as tk

>>> root = tk.Tk()
>>> root.title("Counter")

>>> count = tk.IntVar(value=0)

>>> def increase():
...     count.set(count.get() + 1)
...     label.config(text=str(count.get()))

>>> label = tk.Label(
...     root, text="0", font=("Helvetica", 48)
... )
>>> label.pack()
>>> button = tk.Button(
...     root, text="Increase", command=increase
... )
>>> button.pack()

>>> root.mainloop()

This example demonstrates how to build a basic counter app using tkinter. Each button press updates the displayed value dynamically.

Python GUI Programming With Tkinter

Tutorial

Python GUI Programming: Your Tkinter Tutorial

Complete an interactive tutorial for Python's GUI library Tkinter. Add buttons, text boxes, widgets, event handlers, and more while building two GUI apps.

basics gui stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Aug. 3, 2026