Scanning Barcodes in Python with Tkinter

Barcodes are widely used for inventory management, product tracking, and point-of-sale systems. In this article, we will explore how to create a Python application with a graphical user interface (GUI) using Tkinter that can scan barcodes from a webcam or connected camera. We will use the pyzbar library for barcode decoding.

Prerequisites:

  1. Python: Ensure you have Python installed on your system. You can download it from the official website (https://www.python.org/downloads/).
  2. Tkinter: Tkinter is included with most Python installations, so you don’t need to install it separately.
  3. pyzbar library: Install the pyzbar library to decode barcodes. You can install it using pip:
pip install pyzbar

opencv-python library: Install the opencv-python library to work with the camera. You can install it using pip:

pip install opencv-python

Creating the Barcode Scanner Application:

Step 1: Import the Required Modules

Start by importing the necessary modules:

import tkinter as tk
from tkinter import filedialog
import cv2
from pyzbar.pyzbar import decode
from PIL import Image, ImageTk

Step 2: Create the Tkinter Application

Next, create the main Tkinter window for your barcode scanner application:

root = tk.Tk()
root.title("Barcode Scanner")
root.geometry("800x600")

Step 3: Create GUI Elements

Create the necessary GUI elements: labels, buttons, and an area to display the camera feed:

# Label for displaying the barcode result
result_label = tk.Label(root, text="Scan a barcode", font=("Arial", 16))
result_label.pack(pady=20)

# Canvas to display camera feed
canvas = tk.Canvas(root, width=600, height=400)
canvas.pack()

# Button to start scanning
start_scan_button = tk.Button(root, text="Start Scanning", command=start_scanning)
start_scan_button.pack()

Step 4: Implement Barcode Scanning

Define the function to start the barcode scanning process:

def start_scanning():
    # Open the camera
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()

        if not ret:
            break

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

        # Decode barcodes in the frame
        decoded_objects = decode(frame)

        for obj in decoded_objects:
            barcode_data = obj.data.decode('utf-8')
            result_label.config(text=f"Barcode: {barcode_data}")
        
        # Display the camera feed in the Tkinter window
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        img_tk = ImageTk.PhotoImage(image=img)
        canvas.create_image(0, 0, image=img_tk, anchor=tk.NW)
        root.update()

    # Release the camera
    cap.release()
    result_label.config(text="Scan a barcode")

Step 5: Run the Tkinter Application

Add the code to start the Tkinter application’s main loop:

root.mainloop()

You have now created a simple Python application using Tkinter that can scan barcodes from a camera feed. When you click the “Start Scanning” button, the application will access the camera, continuously capture frames, and display any detected barcodes with their data in the Tkinter window. You can expand on this foundation to create a more sophisticated barcode scanning application, such as one that saves scanned barcode data to a file or sends it to a database for inventory management.