Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #1

Open
mankotia412vishal opened this issue Nov 14, 2024 · 0 comments
Open

Test #1

mankotia412vishal opened this issue Nov 14, 2024 · 0 comments

Comments

@mankotia412vishal
Copy link
Owner

import os
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
import sqlite3
from PIL import Image, ImageTk
from barcode import EAN13
from barcode.writer import ImageWriter
from reportlab.pdfgen import canvas

Database setup

def init_db():
conn = sqlite3.connect('barcode_data.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS barcodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
data TEXT NOT NULL,
barcode_file TEXT NOT NULL
)
''')
conn.commit()
conn.close()

Function to generate barcode and save data

def generate_barcode():
data = entry_data.get()

if len(data) != 12 or not data.isdigit():
    messagebox.showerror("Error", "Please enter a valid 12-digit number.")
    return

# Generate barcode
my_code = EAN13(data, writer=ImageWriter())

# Ensure the directory exists
file_dir = os.path.dirname(os.path.abspath(__file__))  # Current directory
file_path = os.path.join(file_dir, f'bar_code_{data}.png')  # Save path with full path

# Save the barcode
my_code.save(file_path)

# Show barcode on GUI
img = Image.open(file_path)
img = img.resize((300, 150))  # Resize for better display
img_tk = ImageTk.PhotoImage(img)
lbl_barcode.config(image=img_tk)
lbl_barcode.image = img_tk

# Save data and barcode path to database
conn = sqlite3.connect('barcode_data.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO barcodes (data, barcode_file) VALUES (?, ?)', (data, file_path))
conn.commit()
conn.close()

messagebox.showinfo("Success", f"Barcode for '{data}' generated and saved!")

Function to print barcode as PDF

def print_barcode():
data = entry_data.get()

if len(data) != 12 or not data.isdigit():
    messagebox.showerror("Error", "Please generate a barcode before printing.")
    return

pdf_path = f'{data}_barcode.pdf'
c = canvas.Canvas(pdf_path)
c.drawImage(f'bar_code_{data}.png', 100, 700, width=300, height=150)
c.drawString(100, 680, f"Barcode for: {data}")
c.save()

messagebox.showinfo("Success", f"Barcode printed and saved as PDF: {pdf_path}")

Function to save barcode as an image file

def save_barcode():
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
if file_path:
data = entry_data.get()
img = Image.open(f'bar_code_{data}.png')
img.save(file_path)
messagebox.showinfo("Saved", f"Barcode saved as {file_path}")

GUI setup

app = tk.Tk()
app.title("Barcode Generator")
app.geometry("500x400")

Input field

lbl_data = tk.Label(app, text="Enter 12-Digit Number for Barcode:")
lbl_data.pack(pady=10)
entry_data = tk.Entry(app, width=40)
entry_data.pack(pady=5)

Generate button

btn_generate = tk.Button(app, text="Generate Barcode", command=generate_barcode)
btn_generate.pack(pady=10)

Display barcode

lbl_barcode = tk.Label(app)
lbl_barcode.pack(pady=10)

Print button

btn_print = tk.Button(app, text="Print Barcode", command=print_barcode)
btn_print.pack(pady=10)

Save button

btn_save = tk.Button(app, text="Save Barcode as Image", command=save_barcode)
btn_save.pack(pady=10)

Initialize the database

init_db()

Run the app

app.mainloop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant