tkinter

The Python tkinter module provides a powerful object-oriented interface to the 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:

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
  • Supports theming and widget customization through ttk
  • 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:

Python
>>> import tkinter as tk

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

>>> root.mainloop()

Adding a button with a callback function:

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:

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.

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

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


By Leodanis Pozo Ramos • Updated July 24, 2025