QR Code Generator – Python Source Code

  • Code
  • Code With GUI
Python
# Importing required modules
import qrcode # Module for generating QR codes
import webcolors # Module for working with color names

# Function to check if a color name is valid
def check_valid_color(color):
    try:
        webcolors.name_to_rgb(color)
        return True
    except ValueError:
        return False

# Function to customize QR code colors and generate the image
def colour_customization(qr, qrcode_color, qrcode_background_color, qrcode_name):
    # Create the QR code image with specified colors
    img = qr.make_image(fill_color=qrcode_color, back_color=qrcode_background_color)
    img.save(f"{qrcode_name}.png")

# Get user input for link and QR code name
user_link = input("\nEnter the link for QR code creation : ")
qrcode_name = input("Enter the QR code name : ") 

# Ask if the user wants to customize QR code colors
while True:
    qr_color_customize = input("Do you want to customize your QR Code color? (yes/no): ").lower()
    if qr_color_customize in ["yes", "no"]:
        break
    else:
        print(" Please answer with 'yes' or 'no' ")

# Create a QR code object with specified settings
qr = qrcode.QRCode(version=1, 
                       error_correction=qrcode.constants.ERROR_CORRECT_H,
                       box_size=20, 
                       border=2)
qr.add_data(user_link)                                                                                                                                                                               
qr.make(fit=True)

# If user chooses to customize colors
if qr_color_customize=="yes":
    while True:
        qrcode_color = input("Please enter the color you want for the QR code: ")
        if check_valid_color(qrcode_color):
            break
        else:
            print("*Invalid color entered. Please provide valid color names.")
    
    while True:
        qrcode_background_color = input("Please enter the color you want for the QR code Background: ")
        if check_valid_color(qrcode_background_color):
            break
        else:
            print("*Invalid color entered. Please provide valid color names.")
    # Call the customization function to generate QR code with custom colors
    colour_customization(qr, qrcode_color, qrcode_background_color, qrcode_name) 
else:
    # Generate and save the default QR code image
    img = qr.make_image()
    img.save(f"{qrcode_name}.png")

# QR Code Generator Source Code - By Codotix
Python
# Importing required modules
import qrcode 
import tkinter as tk
from tkinter import messagebox, colorchooser

# Function to customize QR code colors and generate the image
def colour_customization():
    # Ask user to choose QR code color
    color_result = colorchooser.askcolor(title="Choose QR Code Color")
    if color_result[1] is not None:
        qrcode_color = color_result[0]  

        # Ask user to choose QR code background color
        bg_color_result = colorchooser.askcolor(title="Choose QR Code Background Color")
        if bg_color_result[1] is not None:
            qrcode_background_color = bg_color_result[0]

            # Create a QR code object
            qr = qrcode.QRCode(version=1, 
                               error_correction=qrcode.constants.ERROR_CORRECT_H,
                               box_size=20, 
                               border=2)
            qr.add_data(user_link.get())   # Get user input link                                                                                                                                                                             
            qr.make(fit=True)

            # Generate the QR code image with customized colors
            img = qr.make_image(fill_color=qrcode_color, back_color=qrcode_background_color)
            img.save(f"{qrcode_name.get()}.png")
            messagebox.showinfo("QR Code Generated", "Customized QR code saved successfully!")

# Function to generate a default QR code
def generate_default_qr():
    qr = qrcode.QRCode(version=1, 
                       error_correction=qrcode.constants.ERROR_CORRECT_H,
                       box_size=20, 
                       border=2)
    qr.add_data(user_link.get())  # Get user input link                                                                                                                                                                               
    qr.make(fit=True)
    
    # Generate the default QR code image
    img = qr.make_image()
    img.save(f"{qrcode_name.get()}.png")
    messagebox.showinfo("QR Code Generated", "Default QR code saved successfully!")

# Create the main application window
root = tk.Tk()
root.title("QR Code Generator")
root.geometry("600x500")

# Create labels and entry fields for user input
link_label = tk.Label(root, text="Enter the link for QR code creation:", font="arial 20 bold")
link_label.pack(pady=(20, 0))

user_link = tk.Entry(root, font="arial 20")
user_link.pack()

name_label = tk.Label(root, text="Enter the QR code name:", font="arial 20 bold")
name_label.pack(pady=(20, 0))

qrcode_name = tk.Entry(root, font="arial 20")
qrcode_name.pack()

# Create buttons to trigger actions
color_customize_button = tk.Button(root, text="Customize QR Code Color", font="arial 15 bold",
                                   command=colour_customization)
color_customize_button.pack(pady=(20, 0))

generate_button = tk.Button(root, text="Generate Default QR Code", font="arial 15 bold", 
                            command=generate_default_qr)
generate_button.pack(pady=(20, 0))

# Start the main event loop
root.mainloop()

Leave a Comment

Your email address will not be published. Required fields are marked *