Affichage des articles dont le libellé est Python Tkinter Table. Afficher tous les articles
Affichage des articles dont le libellé est Python Tkinter Table. Afficher tous les articles

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











Paginated Table Using Tkinter In Python

How To Create a Paginated Table Using Tkinter In Python

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.




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()



The Final Result:

How to Create a Table with Pagination Buttons in Python Tkinter