For creating a table with pagination in tkinter, you can use the ttk.Treeview widget along with the ttk.Paginator class. Here is the step-by-step guide:
- Import the required modules:
import tkinter as tk
from tkinter import ttk
from ttkthemes import ThemedStyle
- Create a tkinter window and set its properties:
root = tk.Tk()
root.title("Table with Pagination")
root.geometry("600x400")
- Create a ttk.Treeview widget and add columns to it:
table = ttk.Treeview(root)
table['columns'] = ("ID", "Name", "Age", "Gender")
table.column("#0", width=0, stretch=tk.NO)
table.column("ID", width=50, anchor=tk.CENTER)
table.column("Name", width=150, anchor=tk.W)
table.column("Age", width=50, anchor=tk.CENTER)
table.column("Gender", width=100, anchor=tk.CENTER)
table.heading("#0", text="", anchor=tk.W)
table.heading("ID", text="ID", anchor=tk.CENTER)
table.heading("Name", text="Name", anchor=tk.CENTER)
table.heading("Age", text="Age", anchor=tk.CENTER)
table.heading("Gender", text="Gender", anchor=tk.CENTER)
- Add data to the table:
for i in range(1, 21):
table.insert(parent='', index=i, iid=i, text="", values=(i, f"Person {i}", i+20, "Male" if i%2==0 else "Female"))
- Create a ttk.Paginator object and set its properties:
paginator = ttk.Paginator(table, 5)
paginator.pack(side=tk.BOTTOM, fill=tk.X, pady=5)
paginator.set_total(len(table.get_children()))
- Create a function to update the table when pagination button is clicked:
def update_table(page):
start_index = (page-1)*5+1
end_index = page*5+1
table.delete(*table.get_children())
for i in range(start_index, end_index):
table.insert(parent='', index=i, iid=i, text="", values=(i, f"Person {i}", i+20, "Male" if i%2==0 else "Female"))
- Create buttons for pagination:
first_btn = tk.Button(root, text='<<', command=lambda: update_table(paginator.first_page()), state="disabled")
first_btn.pack(side=tk.LEFT)
prev_btn = tk.Button(root, text='<', command=lambda: update_table(paginator.previous_page()), state="disabled")
prev_btn.pack(side=tk.LEFT)
current_page_lbl = tk.Label(root, text=f"{paginator.current_page}/{paginator.num_pages}")
current_page_lbl.pack(side=tk.LEFT, padx=(5,5))
next_btn = tk.Button(root, text='>', command=lambda: update_table(paginator.next_page()))
next_btn.pack(side=tk.LEFT)
last_btn = tk.Button(root, text='>>', command=lambda: update_table(paginator.last_page()))
last_btn.pack(side=tk.LEFT)
- Create a function to update the pagination buttons based on current page and total pages:
def update_pagination_buttons():
if paginator.current_page == 1:
first_btn.config(state="disabled")
prev_btn.config(state="disabled")
else:
first_btn.config(state="normal")
prev_btn.config(state="normal")
if paginator.current_page == paginator.num_pages:
next_btn.config(state="disabled")
last_btn.config(state="disabled")
else:
next_btn.config(state="normal")
last_btn.config(state="normal")
current_page_lbl.config(text=f"{paginator.current_page}/{paginator.num_pages}")
- Call the update_table and update_pagination_buttons functions initially and bind the pagination buttons to these functions:
update_table(paginator.current_page)
update_pagination_buttons()
paginator.on_change_page(update_table)
paginator.on_change_page(update_pagination_buttons)
- Apply a ttk style to the window:
style = ThemedStyle(root)
style.set_theme("equilux")
- Run the window by calling the mainloop function:
root.mainloop()
The complete code for creating a table with pagination in Tkinter can be found below: