import tkinter as tk
import tkinter.filedialog as fd
from tkinter import messagebox
import qrcode
from PIL import Image, ImageDraw, ImageFont
class QrCodeGenerator:
def __init__(self, master):
self.master = master
self.master.title("QR Code Generator")
self.master.geometry("500x500")
self.master.resizable(False, False)
self.filename = ""
self.logo_filename = ""
self.img_format = tk.StringVar()
self.img_format.set("PNG")
self.img_size = tk.IntVar()
self.img_size.set(500)
self.img_quality = tk.IntVar()
self.img_quality.set(100)
self.logo_size = tk.IntVar()
self.logo_size.set(100)
self.create_widgets()
def create_widgets(self):
input_frame = tk.LabelFrame(self.master, text="Input File")
input_frame.pack(fill="both", expand=True, padx=10, pady=10)
input_label = tk.Label(input_frame, text="Select File:")
input_label.pack(side="left", padx=10, pady=10)
self.input_entry = tk.Entry(input_frame, width=40)
self.input_entry.pack(side="left", padx=10, pady=10)
input_button = tk.Button(input_frame, text="Browse", command=self.browse_input_file)
input_button.pack(side="left", padx=10, pady=10)
logo_frame = tk.LabelFrame(self.master, text="Logo File")
logo_frame.pack(fill="both", expand=True, padx=10, pady=10)
logo_label = tk.Label(logo_frame, text="Select Logo File (Optional):")
logo_label.pack(side="left", padx=10, pady=10)
self.logo_entry = tk.Entry(logo_frame, width=40)
self.logo_entry.pack(side="left", padx=10, pady=10)
logo_button = tk.Button(logo_frame, text="Browse", command=self.browse_logo_file)
logo_button.pack(side="left", padx=10, pady=10)
format_frame = tk.LabelFrame(self.master, text="Output Format")
format_frame.pack(fill="both", expand=True, padx=10, pady=10)
png_radio = tk.Radiobutton(format_frame, text="PNG", variable=self.img_format, value="PNG")
png_radio.pack(side="left", padx=10, pady=10)
pdf_radio = tk.Radiobutton(format_frame, text="PDF", variable=self.img_format, value="PDF")
pdf_radio.pack(side="left", padx=10, pady=10)
size_frame = tk.LabelFrame(self.master, text="Image Size")
size_frame.pack(fill="both", expand=True, padx=10, pady=10)
size_label = tk.Label(size_frame, text="Enter Image Size (pixels):")
size_label.pack(side="left", padx=10, pady=10)
self.size_entry = tk.Entry(size_frame, width=10, textvariable=self.img_size)
self.size_entry.pack(side="left", padx=10, pady=10)
quality_frame = tk.LabelFrame(self.master, text="Image Quality")
quality_frame.pack(fill="both", expand=True, padx=10, pady=10)
quality_label = tk.Label(quality_frame, text="Enter Image Quality (1-100):")
quality_label.pack(side="left", padx=10, pady=10)
self.quality_entry = tk.Entry(quality_frame, width=10, textvariable=self.img_quality)
self.quality_entry.pack(side="left", padx=10, pady=10)
logo_size_frame = tk.LabelFrame(self.master, text="Logo Size (Optional)")
logo_size_frame.pack(fill="both", expand=True, padx=10, pady=10)
logo_size_label = tk.Label(logo_size_frame, text="Enter Logo Size (pixels):")
logo_size_label.pack(side="left", padx=10, pady=10)
self.logo_size_entry = tk.Entry(logo_size_frame, width=10, textvariable=self.logo_size)
self.logo_size_entry.pack(side="left", padx=10, pady=10)
generate_button = tk.Button(self.master, text="Generate QR Code", command=self.generate_qr_code)
generate_button.pack(fill="x", padx=10, pady=10)
def browse_input_file(self):
self.filename = fd.askopenfilename(title="Select Input File", filetypes=[("All Files", "*.*")])
if self.filename:
self.input_entry.delete(0, tk.END)
self.input_entry.insert(0, self.filename)
def browse_logo_file(self):
self.logo_filename = fd.askopenfilename(title="Select Logo File", filetypes=[("All Files", "*.*")])
if self.logo_filename:
self.logo_entry.delete(0, tk.END)
self.logo_entry.insert(0, self.logo_filename)
def generate_qr_code(self):
if not self.filename:
messagebox.showerror("Error", "Please select an input file")
return
if not self.img_format.get():
messagebox.showerror("Error", "Please select an output format")
return
if not self.img_size.get():
messagebox.showerror("Error", "Please enter an image size")
return
if not self.img_quality.get():
messagebox.showerror("Error", "Please enter an image quality")
return
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(self.filename)
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white")
if self.logo_filename:
logo_img = Image.open(self.logo_filename).convert("RGBA")
logo_size = self.logo_size.get()
if not logo_size:
logo_size = 100
logo_img = logo_img.resize((logo_size, logo_size), Image.ANTIALIAS)
qr_width, qr_height = qr_img.size
logo_width, logo_height = logo_img.size
logo_pos = ((qr_width - logo_width) // 2, (qr_height - logo_height) // 2)
qr_img.paste(logo_img, logo_pos, logo_img)
img_size = self.img_size.get()
img_quality = self.img_quality.get()
if self.img_format.get() == "PDF":
qr_img.save("qrcode.pdf", "PDF", quality=img_quality)
messagebox.showinfo("Success", "QR Code generated successfully!")
else:
qr_img = qr_img.resize((img_size, img_size), Image.ANTIALIAS)
qr_img.save("qrcode.png", "PNG", quality=img_quality)
messagebox.showinfo("Success", "QR Code generated successfully!")
def main():
root = tk.Tk()
app = QrCodeGenerator(root)
root.mainloop()
if __name__ == '__main__':
main()