Table of widgets in Python with Tkinter

Tkinter is a popular library in Python for creating graphical user interfaces (GUIs). It provides a wide range of widgets that can be used to build interactive and visually appealing applications. One of the key features of Tkinter is the ability to create tables or grids of widgets using the grid() geometry manager.

Tkinter provides a widget called Frame that serves as a container for other widgets. We can use frames to create a table-like structure by arranging multiple widgets in rows and columns. Each cell can contain different widgets such as labels, buttons, entry fields, or even other frames.

Basic Table Structure with Labels

Let's start by creating a basic table structure with labels as the cells ?

import tkinter as tk

root = tk.Tk()
root.title("Table-like structure with Labels")
root.geometry("400x200")

# Create a frame to hold the table
table_frame = tk.Frame(root)
table_frame.pack(padx=20, pady=20)

labels = []
for i in range(3):
    row_labels = []
    for j in range(3):
        # Create a label for each cell in the table
        label = tk.Label(table_frame, text=f"Cell {i}-{j}", 
                        relief="solid", borderwidth=1, width=10)
        label.grid(row=i, column=j, padx=2, pady=2)
        row_labels.append(label)
    labels.append(row_labels)

root.mainloop()

In this code, we create a Frame called table_frame to hold our table. We iterate over rows and columns to create labels for each cell. The grid() method positions the labels in their respective positions, and we add borders to make the table structure visible.

Table with Interactive Buttons

Now let's enhance our table by adding buttons that respond to clicks ?

import tkinter as tk

def button_click(row, col):
    print(f"Button clicked: Row {row}, Column {col}")

root = tk.Tk()
root.title("Interactive Button Table")
root.geometry("400x200")

table_frame = tk.Frame(root)
table_frame.pack(padx=20, pady=20)

buttons = []
for i in range(3):
    row_buttons = []
    for j in range(3):
        # Create a button for each cell in the table
        button = tk.Button(table_frame, text=f"Btn {i}-{j}", 
                          command=lambda r=i, c=j: button_click(r, c),
                          width=8, height=2)
        button.grid(row=i, column=j, padx=2, pady=2)
        row_buttons.append(button)
    buttons.append(row_buttons)

root.mainloop()

Here we define a button_click() function that takes row and column indices as arguments. We use a lambda function to pass the current row and column indices to the callback function when each button is clicked.

Dynamic Table Updates

Tables often need to be updated dynamically based on user input. Here's an example that updates label text when a button is clicked ?

import tkinter as tk

root = tk.Tk()
root.title("Dynamic Label Update")
root.geometry("400x150")

table_frame = tk.Frame(root)
table_frame.pack(padx=20, pady=20)

# Create a label with initial text
label = tk.Label(table_frame, text="Original Text", 
                relief="solid", borderwidth=1, width=20, height=2)
label.grid(row=0, column=0, padx=10, pady=10)

def update_label():
    label.config(text="Text Updated!", bg="lightgreen")

# Button to trigger the update
button = tk.Button(table_frame, text="Update Label", 
                  command=update_label, width=15)
button.grid(row=1, column=0, padx=10, pady=10)

root.mainloop()

The update_label() function uses the config() method to modify the label's text and background color properties dynamically.

Mixed Widget Table

You can combine different widget types in a single table structure ?

import tkinter as tk

root = tk.Tk()
root.title("Mixed Widget Table")
root.geometry("500x200")

table_frame = tk.Frame(root)
table_frame.pack(padx=20, pady=20)

# Row 0: Labels
widgets = []
for j in range(3):
    label = tk.Label(table_frame, text=f"Header {j+1}", 
                    font=("Arial", 10, "bold"), bg="lightgray")
    label.grid(row=0, column=j, padx=5, pady=5, sticky="ew")
    widgets.append(label)

# Row 1: Entry fields
for j in range(3):
    entry = tk.Entry(table_frame, width=15)
    entry.grid(row=1, column=j, padx=5, pady=5)
    widgets.append(entry)

# Row 2: Buttons
for j in range(3):
    button = tk.Button(table_frame, text=f"Action {j+1}", width=12)
    button.grid(row=2, column=j, padx=5, pady=5)
    widgets.append(button)

root.mainloop()

This example demonstrates a table with headers (labels), input fields (entries), and action buttons, creating a form-like interface.

Key Points

  • Use Frame widgets as containers for organizing table structures
  • The grid() geometry manager is ideal for table layouts
  • Lambda functions help pass parameters to callback functions
  • Use config() method to update widget properties dynamically
  • Combine different widget types for versatile table interfaces

Conclusion

Tkinter's grid system makes it easy to create table-like structures with various widgets. You can build both static and dynamic tables that respond to user interactions, making them perfect for data entry forms, control panels, and interactive applications.

---
Updated on: 2026-03-27T16:15:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements