QR Code and Barcode Generator in Python (With GUI)
In this tutorial, we will build a QR Code and Barcode Generator using Python with a graphical user interface.
Users can enter personal details like name, roll number, blood group, class, section, and expiry year to generate QR codes or barcodes.
1. Requirements
Make sure Python is installed on your system.
Download Python: https://www.python.org/downloads/
Install required libraries using pip:
pip install pillow qrcode python-barcode
2. Steps to Run the Project
Step 1: Install Python IDE from the official website.
Step 2: Copy and paste the given code into your IDE.
Step 3: Run the program.
Step 4: Enter user details such as name, roll number, blood group, class, section, expiry year, and optional URL.
Step 5: Choose to generate QR Code or Barcode and save it as an image.
3. Python Code
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from PIL import Image, ImageTk
import qrcode
import barcode
from barcode.writer import ImageWriter
qr_image = None
barcode_image = None
def generate_qr_code():
global qr_image
name = name_entry.get()
roll_number = roll_number_entry.get()
blood_group = blood_group_entry.get()
student_class = class_entry.get()
section = section_entry.get()
expiry_year = expiry_year_entry.get()
url = url_entry.get()
if not (name and roll_number and blood_group and student_class and section and expiry_year):
messagebox.showerror("Input Error", "All fields must be filled in.")
return
data = f"Name: {name}\nRoll No: {roll_number}\nBlood Group: {blood_group}\nClass: {student_class}\nSection: {section}\nExpiry Year: {expiry_year}"
if url:
data += f"\nURL: {url}"
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
qr_image = qr.make_image(fill='black', back_color='white')
display_image(qr_image)
def generate_barcode():
global barcode_image
roll_number = roll_number_entry.get()
if len(roll_number) == 12:
barcode_obj = barcode.get('upc', roll_number, writer=ImageWriter())
elif len(roll_number) == 10:
barcode_obj = barcode.get('ean13', roll_number.zfill(12), writer=ImageWriter())
else:
messagebox.showerror("Input Error", "Roll Number must be exactly 10 or 12 digits.")
return
barcode_image = barcode_obj.render()
display_image(barcode_image)
def display_image(img):
img.thumbnail((300, 300))
img_tk = ImageTk.PhotoImage(img)
panel.config(image=img_tk)
panel.image = img_tk
def save_image():
global qr_image, barcode_image
if qr_image is None and barcode_image is None:
messagebox.showerror("Error", "No image generated to save.")
return
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
if file_path:
if qr_image:
qr_image.save(file_path)
messagebox.showinfo("Saved", f"QR Code saved as {file_path}")
elif barcode_image:
barcode_image.save(file_path)
messagebox.showinfo("Saved", f"Barcode saved as {file_path}")
root = tk.Tk()
root.title("QR Code and Barcode Generator")
name_entry = tk.Entry(root, width=40)
name_entry.pack()
roll_number_entry = tk.Entry(root, width=40)
roll_number_entry.pack()
blood_group_entry = tk.Entry(root, width=40)
blood_group_entry.pack()
class_entry = tk.Entry(root, width=40)
class_entry.pack()
section_entry = tk.Entry(root, width=40)
section_entry.pack()
expiry_year_entry = tk.Entry(root, width=40)
expiry_year_entry.pack()
url_entry = tk.Entry(root, width=40)
url_entry.pack()
tk.Button(root, text="Generate QR Code", command=generate_qr_code).pack()
tk.Button(root, text="Generate Barcode", command=generate_barcode).pack()
tk.Button(root, text="Save Image", command=save_image).pack()
panel = tk.Label(root)
panel.pack()
root.mainloop()
4. Output
The application opens a GUI window where users can enter details and generate QR codes or barcodes.
Generated images can be saved as PNG files.
QR codes can be scanned using mobile devices to view stored information.
Conclusion
This project demonstrates how to build a real-world Python GUI application using Tkinter.
It also helps understand QR code generation, barcode creation, and file handling in Python.
Codecrown