Grocery Management System Project in Python with Tkinter | Source Code

Faraz

By Faraz -

Learn how to build a Grocery Management System in Python using Tkinter. Get the complete source code and step-by-step guide to manage your grocery inventory efficiently.


grocery-management-system-project-in-python-with-tkinter-source-code.webp

Table of Contents

  1. Project Overview
  2. Setting Up the Environment
  3. Understanding Tkinter
  4. Building the Grocery Management System
  5. Full Grocery Management System Project Source Code
  6. Deploying the Application
  7. Conclusion

Managing grocery inventory can be a complex task, especially without the right tools. This project aims to simplify the process by providing a complete Grocery Management System built in Python using the Tkinter library. Whether you're a developer looking to enhance your skills or a student working on a practical project, this guide offers a comprehensive step-by-step approach to building a functional grocery management system. With features like adding, updating, deleting, and searching for grocery items, this project will help you manage your inventory efficiently. Plus, you can download the full source code to get started right away.

Project Overview

Objectives of the Project

The primary goal of this project is to develop a simple yet functional Grocery Management System using Python and Tkinter. This system will allow users to add, update, delete, and view grocery items, ensuring a smooth management process.

Key Features of the System

  • User-Friendly Interface: Easy-to-navigate design for efficient item management.
  • CRUD Operations: Comprehensive Create, Read, Update, and Delete functionalities.
  • Search Functionality: Quick search options to find specific items.
  • Data Persistence: Save and load data using JSON for long-term storage.

Setting Up the Environment

Required Software and Libraries

  • Python 3.x: The programming language used for development.
  • Tkinter: The standard Python library for creating graphical user interfaces.
  • JSON: Used for data storage and retrieval.

Installation Guide

  • Python: Download and install the latest version of Python from the official website.
  • Tkinter: Typically included with Python, but can be installed via pip if missing.
  • JSON: No installation needed as it is a standard library in Python.
pip install tkinter

Understanding Tkinter

What is Tkinter?

Tkinter is Python’s de facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk, providing a powerful toolkit for building desktop applications.

Basics of Tkinter

Tkinter simplifies the process of creating windows, dialogs, buttons, and other GUI elements. Its straightforward syntax makes it ideal for beginners and experienced developers alike.

Building the Grocery Management System

Initializing the Application

Start by importing necessary modules and creating the main application window.

import tkinter as tk
from tkinter import messagebox, ttk
import json

class GroceryApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Grocery Management System")
        self.root.geometry("800x600")
        self.root.configure(bg='#f5f5f5')

Designing the User Interface

The user interface consists of entry fields for item details, buttons for CRUD operations, and a treeview for displaying items.

Creating Widgets

Entry Fields for Item Details

Create entry fields for item name, quantity, and price.

self.item_name = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))
self.quantity = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))
self.price = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))

Buttons for CRUD Operations

Create buttons for adding, deleting, updating, and showing items.

tk.Button(button_frame, text="Add Item", command=self.add_item, bg='#4CAF50', fg='#ffffff').grid(row=0, column=0)
tk.Button(button_frame, text="Delete Item", command=self.delete_item, bg='#f44336', fg='#ffffff').grid(row=0, column=1)
tk.Button(button_frame, text="Update Item", command=self.update_item, bg='#2196F3', fg='#ffffff').grid(row=0, column=2)
tk.Button(button_frame, text="Show Items", command=self.show_items, bg='#FF5722', fg='#ffffff').grid(row=0, column=3)

Treeview for Displaying Items

Create a treeview to display the list of grocery items.

self.tree = ttk.Treeview(tree_frame, columns=("Name", "Quantity", "Price"), show="headings")
self.tree.heading("Name", text="Name")
self.tree.heading("Quantity", text="Quantity")
self.tree.heading("Price", text="Price")
self.tree.grid(row=0, column=0, sticky="nsew")

Implementing CRUD Operations

Adding Items

Define the method to add items to the list.

def add_item(self):
    name = self.item_name.get()
    quantity = self.quantity.get()
    price = self.price.get()

    if name and quantity and price:
        self.items[name] = (quantity, price)
        self.clear_entries()
        self.show_items()
        messagebox.showinfo("Info", f"Item '{name}' added successfully!")
    else:
        messagebox.showwarning("Warning", "Please fill all fields!")

Deleting Items

Define the method to delete items from the list.

def delete_item(self):
    selected_item = self.tree.selection()
    if selected_item:
        item_name = self.tree.item(selected_item)['values'][0]
        if item_name in self.items:
            del self.items[item_name]
            self.clear_entries()
            self.show_items()
            messagebox.showinfo("Info", f"Item '{item_name}' deleted successfully!")
        else:
            messagebox.showwarning("Warning", "Item not found!")
    else:
        messagebox.showwarning("Warning", "Please select an item to delete!")

Updating Items

Define the method to update item details.

def update_item(self):
    selected_item = self.tree.selection()
    if selected_item:
        item_name = self.tree.item(selected_item)['values'][0]
        new_quantity = self.quantity.get()
        new_price = self.price.get()
        if new_quantity and new_price:
            if item_name in self.items:
                self.items[item_name] = (new_quantity, new_price)
                self.clear_entries()
                self.show_items()
                messagebox.showinfo("Info", f"Item '{item_name}' updated successfully!")
            else:
                messagebox.showwarning("Warning", "Item not found!")
        else:
            messagebox.showwarning("Warning", "Please enter new quantity and price!")
    else:
        messagebox.showwarning("Warning", "Please select an item to update!")

Search Functionality

Designing the Search Bar

Create a search entry and button for searching items.

self.search_entry = tk.Entry(button_frame, width=30, borderwidth=2, relief="solid", font=("Helvetica", 12))
tk.Button(button_frame, text="Search", command=self.search_item, bg='#FFC107', fg='#ffffff').grid(row=1, column=2)

Implementing Search Logic

Define the method to search for items based on the search term.

def search_item(self):
    search_term = self.search_entry.get().lower()
    self.tree.delete(*self.tree.get_children())

    for name, (quantity, price) in self.items.items():
        if search_term in name.lower():
            self.tree.insert("", "end", values=(name, quantity, price))

Data Persistence with JSON

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy to read and write.

Saving Data to JSON File

Define the method to save items to a JSON file.

def save_items(self):
    with open(self.file_path, 'w') as f:
        json.dump(self.items, f)
    messagebox.showinfo("Info", "Items saved successfully!")

Loading Data from JSON File

Define the method to load items from a JSON file.

def load_items(self):
    try:
        with open(self.file_path, 'r') as f:
            self.items = json.load(f)
        self.show_items()
    except FileNotFoundError:
        messagebox.showwarning("Warning", "No saved data found.")

Enhancing User Experience

Adding Scrollbars to Treeview

Add vertical and horizontal scrollbars to the treeview for better navigation.

self.v_scroll = tk.Scrollbar(tree_frame, orient="vertical", command=self.tree.yview)
self.h_scroll = tk.Scrollbar(tree_frame, orient="horizontal", command=self.tree.xview)
self.tree.configure(yscrollcommand=self.v_scroll.set, xscrollcommand=self.h_scroll.set)

Implementing Entry Validation

Ensure that the user enters valid data in the entry fields.

Full Grocery Management System Project Source Code

import tkinter as tk
from tkinter import messagebox, ttk
import json

class GroceryApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Grocery Management System")
        self.root.geometry("800x600")
        self.root.configure(bg='#f5f5f5')

        self.items = {}
        self.file_path = "grocery_data.json"

        self.create_widgets()
        self.load_items()

    def create_widgets(self):
        # Frame for entry fields
        entry_frame = tk.Frame(self.root, bg='#ffffff', padx=20, pady=20, borderwidth=2, relief="flat")
        entry_frame.pack(padx=15, pady=10, fill="x")

        # Labels and Entry fields
        tk.Label(entry_frame, text="Item Name:", bg='#ffffff', font=("Helvetica", 12)).grid(row=0, column=0, padx=10,
                                                                                            pady=5, sticky="w")
        tk.Label(entry_frame, text="Quantity:", bg='#ffffff', font=("Helvetica", 12)).grid(row=1, column=0, padx=10,
                                                                                           pady=5, sticky="w")
        tk.Label(entry_frame, text="Price:", bg='#ffffff', font=("Helvetica", 12)).grid(row=2, column=0, padx=10,
                                                                                        pady=5, sticky="w")

        self.item_name = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))
        self.item_name.grid(row=0, column=1, padx=10, pady=5)
        self.quantity = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))
        self.quantity.grid(row=1, column=1, padx=10, pady=5)
        self.price = tk.Entry(entry_frame, width=40, borderwidth=2, relief="solid", font=("Helvetica", 12))
        self.price.grid(row=2, column=1, padx=10, pady=5)

        # Frame for buttons and search input
        button_frame = tk.Frame(self.root, bg='#ffffff', padx=20, pady=10)
        button_frame.pack(padx=15, pady=10, fill="x")

        # Buttons with modern design
        btn_config = {"font": ("Helvetica", 12), "padx": 10, "pady": 5, "width": 14, "bd": 0, "relief": "flat"}
        tk.Button(button_frame, text="Add Item", command=self.add_item, bg='#4CAF50', fg='#ffffff', **btn_config).grid(
            row=0, column=0)
        tk.Button(button_frame, text="Delete Item", command=self.delete_item, bg='#f44336', fg='#ffffff',
                  **btn_config).grid(row=0, column=1)
        tk.Button(button_frame, text="Update Item", command=self.update_item, bg='#2196F3', fg='#ffffff',
                  **btn_config).grid(row=0, column=2)
        tk.Button(button_frame, text="Show Items", command=self.show_items, bg='#FF5722', fg='#ffffff',
                  **btn_config).grid(row=0, column=3)

        # Search entry and button
        tk.Label(button_frame, text="Search:", bg='#ffffff', font=("Helvetica", 12)).grid(row=1, column=0, padx=10,
                                                                                          pady=5, sticky="w")
        self.search_entry = tk.Entry(button_frame, width=30, borderwidth=2, relief="solid", font=("Helvetica", 12))
        self.search_entry.grid(row=1, column=1, padx=10, pady=5)
        tk.Button(button_frame, text="Search", command=self.search_item, bg='#FFC107', fg='#ffffff', **btn_config).grid(
            row=1, column=2, padx=5, pady=5)

        tk.Button(button_frame, text="Save", command=self.save_items, bg='#8BC34A', fg='#ffffff', **btn_config).grid(
            row=2, column=0, padx=5, pady=5)
        tk.Button(button_frame, text="Load", command=self.load_items, bg='#3F51B5', fg='#ffffff', **btn_config).grid(
            row=2, column=1, padx=5, pady=5)

        # Frame for Treeview
        tree_frame = tk.Frame(self.root, bg='#ffffff', padx=20, pady=20)
        tree_frame.pack(padx=15, pady=10, fill="both", expand=True)

        # Treeview for displaying items
        self.tree = ttk.Treeview(tree_frame, columns=("Name", "Quantity", "Price"), show="headings")
        self.tree.heading("Name", text="Name")
        self.tree.heading("Quantity", text="Quantity")
        self.tree.heading("Price", text="Price")
        self.tree.grid(row=0, column=0, sticky="nsew")

        # Bind Treeview selection event to on_treeview_select
        self.tree.bind("<<TreeviewSelect>>", self.on_treeview_select)

        # Scrollbars
        self.v_scroll = tk.Scrollbar(tree_frame, orient="vertical", command=self.tree.yview)
        self.v_scroll.grid(row=0, column=1, sticky="ns")
        self.tree.configure(yscrollcommand=self.v_scroll.set)

        self.h_scroll = tk.Scrollbar(tree_frame, orient="horizontal", command=self.tree.xview)
        self.h_scroll.grid(row=1, column=0, sticky="ew")
        self.tree.configure(xscrollcommand=self.h_scroll.set)

        # Expandable rows and columns
        tree_frame.grid_rowconfigure(0, weight=1)
        tree_frame.grid_columnconfigure(0, weight=1)

    def add_item(self):
        name = self.item_name.get()
        quantity = self.quantity.get()
        price = self.price.get()

        if name and quantity and price:
            self.items[name] = (quantity, price)
            self.clear_entries()
            self.show_items()
            messagebox.showinfo("Info", f"Item '{name}' added successfully!")
        else:
            messagebox.showwarning("Warning", "Please fill all fields!")

    def delete_item(self):
        selected_item = self.tree.selection()
        if selected_item:
            item_name = self.tree.item(selected_item)['values'][0]
            if item_name in self.items:
                del self.items[item_name]
                self.clear_entries()
                self.show_items()
                messagebox.showinfo("Info", f"Item '{item_name}' deleted successfully!")
            else:
                messagebox.showwarning("Warning", "Item not found!")
        else:
            messagebox.showwarning("Warning", "Please select an item to delete!")

    def update_item(self):
        selected_item = self.tree.selection()
        if selected_item:
            item_name = self.tree.item(selected_item)['values'][0]
            new_quantity = self.quantity.get()
            new_price = self.price.get()
            if new_quantity and new_price:
                if item_name in self.items:
                    self.items[item_name] = (new_quantity, new_price)
                    self.clear_entries()
                    self.show_items()
                    messagebox.showinfo("Info", f"Item '{item_name}' updated successfully!")
                else:
                    messagebox.showwarning("Warning", "Item not found!")
            else:
                messagebox.showwarning("Warning", "Please enter new quantity and price!")
        else:
            messagebox.showwarning("Warning", "Please select an item to update!")

    def show_items(self):
        for item in self.tree.get_children():
            self.tree.delete(item)

        for name, (quantity, price) in self.items.items():
            self.tree.insert("", "end", values=(name, quantity, price))

    def search_item(self):
        search_term = self.search_entry.get().lower()
        self.tree.delete(*self.tree.get_children())

        for name, (quantity, price) in self.items.items():
            if search_term in name.lower():
                self.tree.insert("", "end", values=(name, quantity, price))

    def save_items(self):
        with open(self.file_path, 'w') as f:
            json.dump(self.items, f)
        messagebox.showinfo("Info", "Items saved successfully!")

    def load_items(self):
        try:
            with open(self.file_path, 'r') as f:
                self.items = json.load(f)
            self.show_items()
        except FileNotFoundError:
            messagebox.showwarning("Warning", "No saved data found.")

    def clear_entries(self):
        self.item_name.delete(0, tk.END)
        self.quantity.delete(0, tk.END)
        self.price.delete(0, tk.END)
        self.search_entry.delete(0, tk.END)

    def on_treeview_select(self, event):
        selected_item = self.tree.selection()
        if selected_item:
            item_name = self.tree.item(selected_item)['values'][0]
            quantity, price = self.items.get(item_name, ('', ''))
            self.item_name.delete(0, tk.END)
            self.item_name.insert(0, item_name)
            self.quantity.delete(0, tk.END)
            self.quantity.insert(0, quantity)
            self.price.delete(0, tk.END)
            self.price.insert(0, price)

if __name__ == "__main__":
    root = tk.Tk()
    app = GroceryApp(root)
    root.resizable(0,0)
    root.mainloop()

Deploying the Application

Creating an Executable File

Use tools like PyInstaller to create an executable file for the application.

pyinstaller --onefile grocery_app.py

Distributing the Application

Distribute the executable file to users along with necessary instructions.

Conclusion

Creating a Grocery Management System in Python with Tkinter is an excellent way to improve your programming skills while developing a useful application. This project covers essential functionalities such as item addition, updating, deletion, and search, providing a robust solution for managing grocery inventories. By following this guide and utilizing the provided source code, you'll gain valuable experience in Python programming and GUI development. We hope this project inspires you to further explore the capabilities of Python and Tkinter in building practical applications.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺