Barkod Okuma Kamera İle

import cv2
from pyzbar import pyzbar
import tkinter as tk
from tkinter import Label, Button, Entry, Toplevel, StringVar, IntVar, DoubleVar
from tkinter import ttk # ttk modülü tablo oluşturmak için gerekli
from PIL import Image, ImageTk
import pandas as pd
import winsound # Windows için ses kütüphanesi

class BarcodeScannerApp:
def init(self, window, window_title):
self.window = window
self.window.title(window_title)

    self.video_source = 0
    self.vid = cv2.VideoCapture(self.video_source)

    self.canvas = tk.Canvas(window, width=self.vid.get(cv2.CAP_PROP_FRAME_WIDTH), height=self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
    self.canvas.pack()

    self.label = Label(window, text="", font=("Arial", 16))
    self.label.pack(anchor=tk.CENTER, expand=True)

    self.barcodes = set()

    self.delay = 15
    self.update()
    
    self.window.mainloop()

def decode_barcodes(self, frame):
    barcodes = pyzbar.decode(frame)
    for barcode in barcodes:
        barcode_data = barcode.data.decode('utf-8')
        winsound.Beep(1000, 200)  # 1000 Hz frekansında 200 milisaniye süreyle ses çalar

        if barcode_data not in self.barcodes:
            self.barcodes.add(barcode_data)
            self.open_entry_window(barcode_data)
        else:
            self.show_existing_data(barcode_data)
        
        x, y, w, h = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        barcode_type = barcode.type
        text = f"{barcode_data} ({barcode_type})"
        cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        self.label.config(text=text)

def open_entry_window(self, barcode_data):
    entry_window = Toplevel(self.window)
    entry_window.title("Ürün Bilgileri Girin")

    Label(entry_window, text="Barkod:").grid(row=0, column=0, padx=10, pady=10)
    Label(entry_window, text=barcode_data).grid(row=0, column=1, padx=10, pady=10)

    Label(entry_window, text="Ürün Cinsi:").grid(row=1, column=0, padx=10, pady=10)
    Label(entry_window, text="Ürün İsmi:").grid(row=2, column=0, padx=10, pady=10)
    Label(entry_window, text="Adet:").grid(row=3, column=0, padx=10, pady=10)
    Label(entry_window, text="Fiyat:").grid(row=4, column=0, padx=10, pady=10)

    product_type = StringVar()
    product_name = StringVar()
    quantity = IntVar()
    price = DoubleVar()

    Entry(entry_window, textvariable=product_type).grid(row=1, column=1, padx=10, pady=10)
    Entry(entry_window, textvariable=product_name).grid(row=2, column=1, padx=10, pady=10)
    Entry(entry_window, textvariable=quantity).grid(row=3, column=1, padx=10, pady=10)
    Entry(entry_window, textvariable=price).grid(row=4, column=1, padx=10, pady=10)

    Button(entry_window, text="Kaydet", command=lambda: self.save_to_excel(barcode_data, product_type.get(), product_name.get(), quantity.get(), price.get(), entry_window)).grid(row=5, column=0, columnspan=2, pady=10)

def save_to_excel(self, barcode, product_type, product_name, quantity, price, window):
    df = pd.DataFrame([[barcode, product_type, product_name, quantity, price]], columns=["Barkod", "Ürün Cinsi", "Ürün İsmi", "Adet", "Fiyat"])
    try:
        existing_df = pd.read_excel("barkodlar.xlsx")
        df = pd.concat([existing_df, df], ignore_index=True)
    except FileNotFoundError:
        pass
    df.to_excel("barkodlar.xlsx", index=False)
    window.destroy()

def show_existing_data(self, barcode_data):
    try:
        df = pd.read_excel("barkodlar.xlsx")
        if barcode_data in df['Barkod'].values:
            product_info = df[df['Barkod'] == barcode_data]

            existing_window = Toplevel(self.window)
            existing_window.title("Mevcut Ürün Bilgileri")

            cols = list(product_info.columns)
            tree = ttk.Treeview(existing_window, columns=cols, show='headings')

            for col in cols:
                tree.heading(col, text=col)
                tree.column(col, minwidth=0, width=100)

            for index, row in product_info.iterrows():
                tree.insert("", "end", values=list(row))

            tree.pack()

    except FileNotFoundError:
        pass

def update(self):
    ret, frame = self.vid.read()
    if ret:
        self.decode_barcodes(frame)
        self.photo = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
        self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
    self.window.after(self.delay, self.update)

def __del__(self):
    if self.vid.isOpened():
        self.vid.release()

if name == “main”:
root = tk.Tk()
app = BarcodeScannerApp(root, “Barkod Okuyucu”)

Barkdu Okuyor Kaydediyorum tekrar barkod okttugum zaman kayıtlı bilgileri göstermiyor acaba nerede yanlış yapıyorum yardımcı olurmusun çünkü her okuttugum zaman direk yeniden ekleme penceresi acılıyor?

bkz: Soru Sorarken Sıkça Düşülen Hatalar #1 ama #18 de faydali olur

Ilk ve tekrar okumalarda barcodes’un, self.barcodes’un, ve her barcode ve barecode_data’nin degeri ne?