How to Build a POS system in Python

A step by step tutorial for building a Point of Sale (POS) system in Python with full code examples

Updated March 29, 2023

In this tutorial, we will walk you through the basics of creating a simple yet robust POS system using Python programming language.

Before we dive in, let’s first discuss what a POS system is and why it is essential for businesses.

A POS system is a computer-based system that helps businesses manage sales, inventory, and other crucial aspects of their operations. It is used in various industries, including retail stores, restaurants, and hospitality. POS systems help businesses keep track of their sales, automate inventory management, reduce human error, and improve customer experience.

Now that you have a basic understanding of what a POS system is let’s get started with building one. We will be using Python programming language and some open-source libraries such as OpenCV and Tkinter.

Step 1: Setting up the environment

Before we start, we need to set up the environment and install the required libraries. We will be using Python 3, so make sure you have it installed on your system. You can download and install Python 3 from the official website https://www.python.org/downloads/. Once Python is installed, you can install the required libraries using pip. Open your command prompt and run the following commands:

pip install opencv-python
pip install numpy
pip install pillow

Step 2: Building the GUI

The first thing we need to do is create the Graphical User Interface (GUI) for our POS system. We will be using the Tkinter library to create the GUI. Open your text editor or IDE and create a new Python file. Add the following code to create a basic window:

import tkinter as tk

# Create a new window
window = tk.Tk()

# Set the title of the window
window.title("Python POS System")

# Set the size of the window
window.geometry("600x400")

# Run the window
window.mainloop()

This code will create a new window with the title “Python POS System” and a size of 600x400 pixels.

Step 3: Creating the Product Database

Next, we need to create a product database that will store the information about the products we sell. We will use a dictionary to store the product details such as product name, price, and quantity. Add the following code to create the product database:

# Create a product database
products = {
    "101": {"name": "Product 1", "price": 10.00, "quantity": 100},
    "102": {"name": "Product 2", "price": 20.00, "quantity": 50},
    "103": {"name": "Product 3", "price": 30.00, "quantity": 20},
    "104": {"name": "Product 4", "price": 40.00, "quantity": 10},
    "105": {"name": "Product 5", "price": 50.00, "quantity": 5}
}

You can add as many products as you like, just make sure to follow the same format.

Step 4: Creating the Barcode Scanner

Now, we need to create a barcode scanner to scan the product barcode and retrieve the product details from the product database. We will be using the OpenCV library to read the barcode. Add the following code to create the barcode scanner:

import cv2

# Create a new window for the camera feed
cv2.namedWindow("Barcode Scanner")

# Start the camera feed
cap = cv2.VideoCapture(0)

while True:
    # Capture a frame from the camera
    ret, frame = cap.read()

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Create a barcode scanner object
    scanner = cv2.QRCodeDetector()

    # Detect the barcode in the frame
    data, bbox, _ = scanner.detectAndDecode(gray)

    # If a barcode is detected, retrieve the product details
    if bbox is not None:
        # Extract the product ID from the barcode
        product_id = data.strip()

        # Retrieve the product details from the product database
        product = products.get(product_id)

        # If the product is found, display its details
        if product:
            print(f"Product ID: {product_id}")
            print(f"Product Name: {product['name']}")
            print(f"Product Price: {product['price']}")
            print(f"Product Quantity: {product['quantity']}")

    # Display the camera feed in the window
    cv2.imshow("Barcode Scanner", frame)

    # Exit the loop if the 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    #Release the camera and destroy the window
    cap.release()
    cv2.destroyAllWindows()

This code will start the camera feed and display it in a new window called “Barcode Scanner”. It will then detect and decode the barcode in the frame and retrieve the product details from the product database. Finally, it will display the product details on the console.

Step 5: Creating the Billing System

Now that we have the barcode scanner and the product database, we need to create the billing system. We will use the Tkinter library to create the billing system. Add the following code to create the billing system:

# Create a new window for the billing system
billing_window = tk.Toplevel(window)

# Set the title of the billing system
billing_window.title("Billing System")

# Set the size of the billing system
billing_window.geometry("600x400")

# Create a label for the product ID
product_id_label = tk.Label(billing_window, text="Product ID:")
product_id_label.pack()

# Create an entry box for the product ID
product_id_entry = tk.Entry(billing_window)
product_id_entry.pack()

# Create a label for the product name
product_name_label = tk.Label(billing_window, text="Product Name:")
product_name_label.pack()

# Create an entry box for the product name
product_name_entry = tk.Entry(billing_window, state="disabled")
product_name_entry.pack()

# Create a label for the product price
product_price_label = tk.Label(billing_window, text="Product Price:")
product_price_label.pack()

# Create an entry box for the product price
product_price_entry = tk.Entry(billing_window, state="disabled")
product_price_entry.pack()

# Create a label for the product quantity
product_quantity_label = tk.Label(billing_window, text="Product Quantity:")
product_quantity_label.pack()

# Create an entry box for the product quantity
product_quantity_entry = tk.Entry(billing_window, state="disabled")
product_quantity_entry.pack()

# Create a label for the total price
total_price_label = tk.Label(billing_window, text="Total Price:")
total_price_label.pack()

# Create an entry box for the total price
total_price_entry = tk.Entry(billing_window, state="disabled")
total_price_entry.pack()

# Create a button to calculate the total price
calculate_button = tk.Button(billing_window, text="Calculate Total", command=calculate_total)
calculate_button.pack()

# Create a button to clear the entries
clear_button = tk.Button(billing_window, text="Clear Entries", command=clear_entries)
clear_button.pack()

This code will create a new window called “Billing System” with several labels and entry boxes for the product details and the total price. It will also create two buttons: one to calculate the total price and the other to clear the entries.

Step 6: Calculating the Total Price

Next, we need to implement the function to calculate the total price of the product. Add the following code to calculate the total price:

# Define the function to calculate the total price
def calculate_total():
    # Get the product ID from the entry box
    product_id = product_id_entry.get().strip()

    # Retrieve the product details from the product database
    product = products.get(product_id)

    # If the product is found, calculate the total price
    if product:
        # Get the quantity from the entry box
        quantity = int(product_quantity_entry.get())

        # Calculate the total price
        total_price = product['price'] * quantity

        # Update the product name, price, quantity, and total price in the entry boxes
        product_name_entry.config(state="normal")
        product_name_entry.delete(0, tk.END)
        product_name_entry.insert(0, product['name'])
        product_name_entry.config(state="disabled")

        product_price_entry.config(state="normal")
        product_price_entry.delete(0, tk.END)
        product_price_entry.insert(0, product['price'])
        product_price_entry.config(state="disabled")

        product_quantity_entry.config(state="normal")
        product_quantity_entry.delete(0, tk.END)
        product_quantity_entry.insert(0, quantity)
        product_quantity_entry.config(state="disabled")

        total_price_entry.config(state="normal")
        total_price_entry.delete(0, tk.END)
        total_price_entry.insert(0, total_price)
        total_price_entry.config(state="disabled")
    else:
        # Display an error message if the product is not found
        tk.messagebox.showerror("Error", "Product not found")

This code will retrieve the product details from the product database using the product ID entered in the entry box. It will then calculate the total price based on the product price and quantity entered in the respective entry boxes. Finally, it will update the product name, price, quantity, and total price in the entry boxes.

Step 7: Clearing the Entries

Finally, we need to implement the function to clear the entries when the “Clear Entries” button is clicked. Add the following code to clear the entries:

# Define the function to clear the entries
def clear_entries():
    product_id_entry.delete(0, tk.END)
    product_name_entry.config(state="normal")
    product_name_entry.delete(0, tk.END)
    product_name_entry.config(state="disabled")
    product_price_entry.config(state="normal")
    product_price_entry.delete(0, tk.END)
    product_price_entry.config(state="disabled")
    product_quantity_entry.config(state="normal")
    product_quantity_entry.delete(0, tk.END)
    product_quantity_entry.config(state="disabled")
    total_price_entry.config(state="normal")
    total_price_entry.delete(0, tk.END)
    total_price_entry.config(state="disabled")

This code will clear all the entries when the “Clear Entries” button is clicked.

Step 8: Putting It All Together

Now that we have all the components, let’s put it all together. Add the following code to the main function to start the POS system:

# Define the main function
def main():
    # Start the barcode scanner
    barcode_scanner()

    # Run the billing system
    billing_window.mainloop()

# Call the main function
if __name__ == "__main__":
    main()

This code will call the barcode_scanner function to start the barcode scanner and then run the billing system using the mainloop method.

Step 9: Testing the POS System

Congratulations! You have successfully built a simple yet functional POS system using Python. Now it’s time to test it. Run the Python file and follow these steps:

  • Scan a product barcode using the barcode scanner.
  • Enter the quantity of the product in the respective entry box.
  • Click the “Calculate Total” button to calculate the total price.
  • Verify the product details and total price displayed in the entry boxes.
  • Click the “Clear Entries” button to clear the entries and start over.

You can also modify the product database and GUI according to your needs and customize the POS system to your liking.

Conclusion

In this tutorial, we have walked you through the basics of building a simple POS system using Python. We have covered topics such as creating the GUI, creating the product database, creating the barcode scanner, creating the billing system, calculating the total price, and clearing the entries. We have also used open-source libraries such as OpenCV and Tkinter to make the POS system more robust and functional.

We hope you found this tutorial helpful and informative. Remember, practice makes perfect, so keep experimenting with the code and explore the possibilities of Python programming. Happy coding!

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!