.png)
The Stock Records Table provides a convenient overview of all inventory transactions stored in the system. It displays important information such as transaction type (Add or Withdraw), item category, customer details, order date, stock status, quantity, unit price, total value, and product image. Users can select a record from the table to automatically populate the dashboard form for review or updating. This feature makes it easier to track inventory movements, monitor stock levels, and manage records efficiently from a single interface.
Prerequisite:
This tutorial is part of the CustomTkinter Inventory Management System Series.
📚 View the Complete CustomTkinter Inventory Management System Series
Preliminary:python -m venv venv
venv\Scripts\activate
pip install CTkTable ctktablerowselector==1.0.1 pillow
Then, we need to set up the file and folder structure, as below:
Everything is as before, except in this tutorial, I will add and use a table.py file and also connect the window with the table button in the app.py file.from table import open_table
# ---------------- TABLE BUTTON ----------------
# Button used to display inventory records in table view
table_button = ctk.CTkButton(button_frame, text="Table", font=font_bold,
state='disabled', command=lambda: open_table(
root, item_name_entry,
name_entry, address_entry,
email_entry, select_date_label,
radio_var, quantity_entry,
unit_price_entry, price_label,
table_window, picture_label))
table_button.grid(row=0, column=4, padx=10, pady=10)
While setting up the table in the table.py file, and my code is as follows:import customtkinter as ctk
from CTkTable import CTkTable
from CTkTableRowSelector import *
from tinydb import TinyDB
from tinydb.operations import delete
from PIL import Image
import os
db = TinyDB('inventory/inventory.json')
table = None
table_window = None
def open_table(parent, item_name_entry,
name_entry, address_entry,
email_entry, select_date_label,
radio_var, quantity_entry,
unit_price_entry, price_label,
table_window, picture_label):
global table
table_window = ctk.CTkToplevel(parent)
table_window.geometry("1400x600")
table_window.title('Inventory Management System')
ctk.set_appearance_mode('dark')
ctk.set_default_color_theme('inventory/custom_theme.json')
(Ps: the argument that passes to the function from table.py to app.py must be equal; otherwise, it will be shown as an error.)
# Headers for the table
headers = [
"Type", "Item", "Name", "Address",
"Email", "Order Date", "Stock Status",
"Quantity", "Unit Price", "Total value",
"Picture"
]
table_data = [headers]
# Fetch data from the database and
# prepare it for the table
db_data = db.all()
# Loop through all records retrieved from TinyDB
for item in db_data:
# Get the quantity value from the current record
quantity = float(item.get("quantity", "0.00"))
unit_price = float(item.get("unit_price", "0.00"))
# Calculate the total cost
total_cost = quantity * unit_price
# Format the total cost to 2 decimal places
formatted_total_cost = f"{total_cost:.2f}"
# Add the record to the table data list
table_data.append([
item.get("type", ""),
item.get("item", ""),
item.get("name", ""),
item.get("address", ""),
item.get("email", ""),
item.get("order_date", ""),
item.get("stock_status", ""),
item.get("quantity", ""),
item.get("unit_price", ""),
# calculation from above
formatted_total_cost,
item.get("picture", "")
])
# destroy old table
if table is not None:
table.destroy()
# Create the table and add it to the scrollable frame
scroll_frame = ctk.CTkScrollableFrame(
table_window,
width=1300,
height=400
)
scroll_frame.pack(expand=True, fill="both", padx=20, pady=20)
table = CTkTable(
master=scroll_frame,
row=len(table_data),
column=len(headers),
values=table_data,
header_color="blue",
corner_radius=10,
justify="center",
text_color="white"
)
table.pack(expand=True, fill="both", padx=20, pady=20)
# Refresh the table window immediately
table_window.update()
#### Add the selector ####
row_selector = CTkTableRowSelector(table)About the Author
Kelvin Loh is a Python developer focused on Flask, desktop applications, and business automation solutions. He shares practical tutorials and real-world coding projects to help developers and small businesses build useful applications.



Comments
Post a Comment