How To Create a Paginated Table Using Tkinter In Python
In this Python tutorial we will create a paginated table using the Tkinter library for the graphical user interface.
The table shows a list of data and includes pagination controls with "Previous" and "Next" buttons.
These buttons allow you to navigate through different pages of data, updating the rows displayed according to the current page.
What We Are Gonna Use In This Project:
- Python Programming Language.- Tkinter for GUI.
- VS Code Editor.
- VS Code Editor.
Project Source Code:
import tkinter as tk
from tkinter import ttk
class TablePaginationFrame(tk.Tk):
def __init__(self):
super().__init__()
self.title("Paginated Table")
self.geometry("400x300")
self.create_ui()
def create_ui(self):
ttk.Style().configure("TButton", padding=5, relief="flat")
# Create a Treeview widget for displaying the table
self.table_model = ttk.Treeview(self, columns=("ID", "Name"),
show="headings", height=5)
self.table_model.heading("ID", text="ID")
self.table_model.heading("Name", text="Name")
self.table_model.column("ID", width="50")
self.table_model.column("Name", width="50")
self.table_model.tag_configure("oddrow", background="#f0f0f0")
# Create a vertical scrollbar for the table
scroll_y = ttk.Scrollbar(self, orient="vertical",
command=self.table_model.yview)
self.table_model.configure(yscroll=scroll_y.set)
# Create Previous and Next buttons for pagination
self.prev_button = ttk.Button(self, text="Previous", command=self.prev_page)
self.next_button = ttk.Button(self, text="Next", command=self.next_page)
# Grid layout for widgets
self.table_model.grid(row = 0, column = 0, columnspan = 2, padx = 0,
pady = 5, sticky = "nsew")
scroll_y.grid(row = 0, column = 2, sticky="nsew")
self.prev_button.grid(row=1, column=0, padx=5, pady=5, sticky="w")
self.next_button.grid(row=1, column=1, padx=5, pady=5, sticky="e")
# Configure grid weights to make the table expandable
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
# Initialize pagination parameters
self.current_page = 1
self.items_per_page = 10
self.update_table_model()
def prev_page(self):
# Go to the previous page if available
if self.current_page > 1:
self.current_page -= 1
self.update_table_model()
def next_page(self):
# Go to the next page if available
if self.current_page < self.get_total_pages():
self.current_page += 1
self.update_table_model()
def update_table_model(self):
# Clear the current table contents
self.table_model.delete(*self.table_model.get_children())
# Calculate the indices for the start and end of the current page
start_index = (self.current_page - 1) * self.items_per_page
end_index = min(start_index + self.items_per_page, len(self.get_data()))
# Iterate over the data for the current page
for i in range(start_index, end_index):
# Get the values for the current row
values = self.get_data()[i]
# Determine tags for the row to set alternate row background colors
tags = ("oddrow") if i % 2 != 0 else ()
# Insert the row into the table with the calculated values and tags
self.table_model.insert("", "end", values = values, tags=tags)
# Disable/enable pagination buttons based on current page
self.prev_button["state"] = tk.NORMAL if self.current_page > 1
else tk.DISABLED
self.next_button["state"] = tk.NORMAL
if self.current_page < self.get_total_pages() else tk.DISABLED
def get_total_pages(self):
# Calculate total pages based on data length and items per page
return -(-len(self.get_data()) / self.items_per_page)
def get_data(self):
# Mock data for the table
return [
(1, "Apple"), (2, "Banana"), (3, "Orange"), (4, "Grapes"),
(5, "Strawberry"),
(6, "Watermelon"), (7, "Pineapple"), (8, "Mango"), (9, "Peach"),
(10, "Kiwi"),
(11, "Blueberry"), (12, "Cherry"), (13, "Raspberry"), (14, "Pear"),
(15, "Plum"),
(16, "Avocado"), (17, "Coconut"), (18, "Lemon"), (19, "Pomegranate"),
(20, "Papaya"),
(21, "Apricot"), (22, "Blackberry"), (23, "Cantaloupe"),
(24, "Cranberry"), (25, "Fig"),
(26, "Guava"), (27, "Lychee"), (28, "Mango"), (29, "Passion Fruit"),
(30, "Tangerine"),
(31, "Dragon Fruit"), (32, "Kumquat"), (33, "Lime"),
(34, "Nectarine"), (35, "Persimmon"),
(36, "Rambutan"), (37, "Starfruit"), (38, "Ugli Fruit"),
(39, "Honeydew Melon"), (40, "Jackfruit"),
(41, "Lingonberry"), (42, "Mangosteen"), (43, "Olive"),
(44, "Quince"), (45, "Soursop"),
(46, "Tamarillo"), (47, "Ugni"), (48, "Yangmei"), (49, "Zucchini"),
(50, "Elderberry")
]
if __name__ == "__main__":
app = TablePaginationFrame()
app.mainloop()