Custom Table In Python Tkinter

How to Create Custom Table In Python Tkinter

How to Create Custom Table In Python Tkinter


In this Python tutorial we will create a custom table with a fixed header and scrollable content using the Tkinter library for the graphical user interface. 
The table lists names and ages with alternating row colors.

What We Are Gonna Use In This Project:

- Python Programming Language.
- Tkinter for GUI.
- VS Code Editor.




Project Source Code:



import tkinter as tk
from tkinter import ttk


class CustomTable(tk.Tk):
def __init__(self):
super().__init__()
self.title("Custom Table")
self.geometry("400x300")
self.table_data = [
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"), ("John", "Doe", "25"),
("Jane", "Smith", "30"), ("Alice", "Johnson", "22"),
("Bob", "Brown", "35"), ("Eve", "Wilson", "28"),
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"), ("John", "Doe", "25"),
("Jane", "Smith", "30"), ("Alice", "Johnson", "22"),
("Bob", "Brown", "35"), ("Eve", "Wilson", "28"),
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"), ("John", "Doe", "25"),
("Jane", "Smith", "30"), ("Alice", "Johnson", "22"),
("Bob", "Brown", "35"), ("Eve", "Wilson", "28"),
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"), ("John", "Doe", "25"),
("Jane", "Smith", "30"), ("Alice", "Johnson", "22"),
("Bob", "Brown", "35"), ("Eve", "Wilson", "28"),
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"), ("John", "Doe", "25"),
("Jane", "Smith", "30"), ("Alice", "Johnson", "22"),
("Bob", "Brown", "35"), ("Eve", "Wilson", "28"),
("John", "Doe", "25"), ("Jane", "Smith", "30"),
("Alice", "Johnson", "22"), ("Bob", "Brown", "35"),
("Eve", "Wilson", "28"),
]
self.table_frame = TablePanel(self, self.table_data)
self.table_frame.pack(fill=tk.BOTH, expand=True)

class TablePanel(tk.Frame):
def __init__(self, parent, table_data):
super().__init__(parent)

# Fixed header frame
self.fixed_frame = tk.Frame(self)
self.fixed_frame.pack(side=tk.TOP, fill=tk.X)

# Fixed header canvas
self.fixed_canvas = tk.Canvas(self.fixed_frame, bg="#4caf50", height=30)
self.fixed_canvas.pack(fill=tk.X)

# Scrollable table canvas
self.table_canvas = tk.Canvas(self, bg="white", height=300)
self.table_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Scrollbar
self.scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL,
        command=self.table_canvas.yview)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

# Configure canvas scrolling
self.table_canvas.configure(yscrollcommand=self.scrollbar.set)
self.table_data = table_data
self.draw_table()


def draw_table(self):
x, y = 0, 0
row_height = 30
table_width = 400

# Draw fixed header
self.fixed_canvas.create_rectangle(x, y, x + table_width, y + row_height,
        fill="#4caf50")
self.fixed_canvas.create_text(x + 70, y + 15, text="First Name",
        font=("Arial", 14, "bold"), fill="white")
self.fixed_canvas.create_text(x + 190, y + 15, text="Last Name",
        font=("Arial", 14, "bold"), fill="white")
self.fixed_canvas.create_text(x + 320, y + 15, text="Age",
        font=("Arial", 14, "bold"), fill="white")

#y += row_height

for i, row_data in enumerate(self.table_data):
# Alternate row colors
fill_color = "#f9f1ee" if i % 2 == 0 else "#b5f2ee"
# Draw table rows
self.table_canvas.create_rectangle(x, y, x + table_width, y + row_height,
            fill=fill_color)
self.table_canvas.create_text(x + 70, y + 20, text=row_data[0],
            font=("Arial", 12), fill="black")
self.table_canvas.create_text(x + 190, y + 20, text=row_data[1],
            font=("Arial", 12), fill="black")
self.table_canvas.create_text(x + 320, y + 20, text=row_data[2],
            font=("Arial", 12), fill="black")

y += row_height

self.table_canvas.configure(scrollregion=self.table_canvas.bbox("all"))




if __name__ == "__main__":
app = CustomTable()
app.mainloop()



The Final Result:

Custom Table In Python Tkinter