Tkinter library in Python
In this article, we'll dive into the world of Graphical User Interface (GUI) development using Python's Tkinter library. Tkinter stands as a versatile and user-friendly tool for beginners and advanced programmers alike, simplifying the creation of GUI applications. We aim to demystify the process of working with Tkinter, from understanding its core widgets to leveraging its strengths and navigating its limitations. By the end, you'll be equipped with the essentials of Tkinter to enhance your Python GUI development projects.
Understanding Tkinter
Tkinter, short for Tk interface, is a built-in Python library designed for creating interactive GUI applications. Tkinter, built on the Tk GUI toolkit, streamlines the creation of windows, buttons, menus, and other graphical elements. As an integral part of the Python standard library, Tkinter smoothly integrates with Python projects, requiring no extra installations, for a seamless GUI development experience. The official python tkinter documentation offers in-depth explanations of Tkinter modules, classes, and methods. Visit Documentation
Tip: As you embark on your GUI development journey, start by sketching out the design and functionality of your application before jumping into the code. This can save time and streamline your development process.
Exploring Basic Widgets
We'll begin by exploring some fundamental components you can construct using Tkinter. The following snippets must be preceeded by:
import tkinter as tk
window = tk.Tk()
window.geometry("457x335")
and followed by:
window.mainloop()
Caution: When working with widgets, remember to assign a unique name to each widget variable to avoid confusion, especially when your application starts to grow.
Label
Presents either text or an image, lacking interactive features for users. For more information on label refer official documentation of python.
label = tk.Label(window, text="This is a Label")
label.pack()
Button
An interactive component that executes a specific operation upon being clicked, like dispatching form data or activating a particular function.
def button_click():
print("Button clicked")
button = tk.Button(window, text="Click here!", command=button_click)
button.pack()
After clicking the button:
Entry
A text field where users can input text or numbers. This component is frequently utilized for entering information or executing search operations.
entry = tk.Entry(window)
entry.pack()
Text
A text field that provides a multi-line text editing area for users to input or display text.
text = tk.Text(window, height=5, width=30)
text.pack()
Scale
Enables users to choose a value from a spectrum by moving a slider control. It is useful for selecting values like volume, brightness, etc.
def update_value(value):
label.config(text=f"Volume Level: {value}")
scale = tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL, command=update_value) #create a scale to select values from 0 to 100
scale.pack()
label = tk.Label(window, text="Volume Level: ")
label.pack()
Spinbox
A numerical input field with up and down arrow buttons for incrementing or decrementing the value.
spinbox = tk.Spinbox(window, from_=0, to=10)
spinbox.pack()
Separator
A horizontal or vertical line to visually separate sections or groups of widgets.
from tkinter import ttk #for themed widgets
label_1 = tk.Label(window, text="This is above the separator.")
label_1.pack()
separator = ttk.Separator(window, orient=tk.HORIZONTAL)
separator.pack(fill=tk.X)
label_2 = tk.Label(window, text="This is below the separator.")
label_2.pack()
Checkbutton
A checkbox enabling users to toggle between selecting and deselecting an option.
check_var = tk.BooleanVar() #To keep track of the checkbox's state (checked or unchecked)
check_button = tk.Checkbutton(window, text="Checkbox", variable=check_var)
check_button.pack()
Radiobutton
A group of radio buttons, ensuring that users can select only one option from a list of choices.
radio_var = tk.StringVar() #To track which radio button is selected
radio_1 = tk.Radiobutton(window, text="Option 1", variable=radio_var, value="Option 1")
radio_2 = tk.Radiobutton(window, text="Option 2", variable=radio_var, value="Option 2")
radio_1.pack()
radio_2.pack()
Listbox
A widget presenting a list of items, allowing users to select one or more options from the list.
Scrollbar
Permits users to navigate through content surpassing the widget's visible boundaries, like in a text field or list.
listbox = tk.Listbox(window)
scrollbar = tk.Scrollbar(window)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y) #scrollbar will be at the right side of the window and expands vertically
listbox.pack(side=tk.LEFT, fill=tk.BOTH) #listbox will be at the left side of the window and expands horizontally and vertically
listbox.config(yscrollcommand=scrollbar.set) #Attach scrollbar to listbox
scrollbar.config(command=listbox.yview)
for i in range(30):
listbox.insert(tk.END, f"Item {i+1}")
ColorChooser
A dialog for users to select a color. It provides a graphical interface for choosing colors using a palette or entering RGB values.
from tkinter import colorchooser
def choose_color():
color = colorchooser.askcolor()
print("Selected color:", color)
button = tk.Button(window, text="Choose Color", command=choose_color)
button.pack()
Frame
A container used to group and organize other widgets. It is frequently used for layout purposes, creating sections or divisions within a window.
frame = tk.Frame(window, bg="lightblue", bd=5)
frame.pack()
label = tk.Label(frame, text="Inside the Frame", bg="lightblue")
label.pack()
Canvas
Provides a drawing area for creating graphics, shapes, and custom elements. It is used for creating diagrams, charts, and interactive visualizations.
canvas = tk.Canvas(window, width=200, height=100, bg="white")
canvas.pack()
canvas.create_rectangle(50, 25, 150, 75, fill="red") #draw a red-coloured rectangle
PanedWindow
A container for arranging multiple panes or frames with adjustable sizes. It allows users to resize panes by dragging the divider between them.
paned_window = tk.PanedWindow(window, orient=tk.HORIZONTAL)
paned_window.pack(fill=tk.BOTH, expand=True) #will expand both horizontally and vertically
frame1 = tk.Frame(paned_window, width=228, height=200, background="red")
frame2 = tk.Frame(paned_window, width=228, height=200, background="blue")
paned_window.add(frame1)
paned_window.add(frame2)
After dragging the divider to the left:
Menu
Creates a menu bar or dropdown menu with options and commands. It is commonly used to include navigation, settings and additional functionalities into the application's interface.
def print_something():
print("You chose the first option")
menu_bar = tk.Menu(window)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Print Something", command=print_something)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=window.quit)
menu_bar.add_cascade(label="Click to see options", menu=file_menu)
window.config(menu=menu_bar)
After selecting "Print Something":
These elements serve as fundamental components for Tkinter GUI applications, frequently employed across diverse projects to develop intuitive interfaces.
To delve deeper into Tkinter and explore its capabilities, you can start with the following sections of the Python documentation:
- Tkinter — Python interface to Tcl/Tk: This section provides the foundational knowledge required to create GUI applications with Tkinter, including widget classes, event handling, and more Python Tkinter Documentation.
- Graphical User Interfaces with Tk: This part of the documentation discusses the integration of Tk/Tcl with Python, offering insights into the architecture of Tkinter applications, the Tkinter module, and thematic widget extensions Python GUI with Tk.
- Tkinter.ttk — Tk themed widgets: For those interested in using themed widgets for a more modern GUI look, this section details the thematic widget set provided by Tk 8.5 and integrated into Tkinter Themed Widgets.
Conclusion
Tkinter is a versatile and user-friendly toolkit for Python GUI development, offering a rich set of widgets, cross-platform compatibility, and seamless integration with Python code. Despite its limitations, Tkinter remains a popular choice for developers looking for efficient creation of simple to moderately complex GUI applications.
Subscribe to my newsletter
Read articles from Jim Smith directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by