Here's a step-by-step guide to building your QR code generator with Tkinter:
Step 1: Install Required Libraries
Open your terminal/command prompt and run:
pip install qrcode[pil]
pip install pillow
Step 2: Create the Python Script
Create a new Python file (e.g., qr_generator.py).
Step 3: Import Required Modules
import tkinter as tk
from tkinter import messagebox
import qrcode
from PIL import Image, ImageTk
Step 4: Set Up the Main Window
root = tk.Tk()
root.title("QR Code Generator")
root.geometry("600x600") # Optional: Set window size
Step 5: Create UI Components
Create frames to organize elements:
# Top frame for buttons
top_frame = tk.Frame(root)
top_frame.pack(pady=10)
# Left frame for website buttons
website_buttons_frame = tk.Frame(top_frame)
website_buttons_frame.pack(side=tk.LEFT, padx=10)
# Right frame for custom input
custom_frame = tk.Frame(top_frame)
custom_frame.pack(side=tk.RIGHT, padx=10)
# Frame to display QR code
qr_frame = tk.Frame(root)
qr_frame.pack(pady=10)
# Status/error message label
status_label = tk.Label(root, text="", fg="red")
status_label.pack(pady=5)
# Label to display QR code image
qr_label = tk.Label(qr_frame)
qr_label.pack()
Step 6: Create the QR Code Generation Function
def generate_qr(url):
# Validate URL
url = url.strip()
if not url:
status_label.config(text="Error: URL cannot be empty")
return
if not (url.startswith("http://") or url.startswith("https://")):
status_label.config(text="Error: URL must start with http:// or https://")
return
# Clear previous error messages
status_label.config(text="")
# Generate QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10, # Adjust size here (smaller number = smaller QR code)
border=4,
)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
# Convert to PhotoImage and display
qr_image = ImageTk.PhotoImage(img)
qr_label.config(image=qr_image)
qr_label.image = qr_image # Keep a reference to prevent garbage collection
Step 7: Add Website Buttons
def create_website_button(parent, text, url):
btn = tk.Button(parent, text=text, command=lambda u=url: generate_qr(u))
btn.pack(pady=5)
# Add buttons for popular websites
create_website_button(website_buttons_frame, "YouTube", "https://www.youtube.com")
create_website_button(website_buttons_frame, "Spotify", "https://www.spotify.com")
create_website_button(website_buttons_frame, "Instagram", "https://www.instagram.com")
create_website_button(website_buttons_frame, "TikTok", "https://www.tiktok.com")
Step 8: Add Custom URL Input
custom_entry = tk.Entry(custom_frame, width=30)
custom_entry.pack(pady=5)
custom_btn = tk.Button(
custom_frame,
text="Generate Custom",
command=lambda: generate_qr(custom_entry.get())
)
custom_btn.pack(pady=5)
Step 9: Start the Tkinter Event Loop
root.mainloop()
Step 10: Run Your Program
Save the file and run it with:
python qr_generator.py
How It Works:
-
UI Layout: The window has three sections:
- Left: Buttons for popular websites
- Right: Custom URL input field
- Bottom: QR code display area
-
QR Code Generation:
- Clicking a website button generates a QR code for its URL.
- Entering a URL in the custom field and clicking "Generate Custom" creates a QR code for that URL.
-
Error Handling:
- Shows an error if the URL is empty or missing
http:///https://.
-
Customization Options:
- Adjust
box_size in the QRCode setup to change QR code size.
- Add more websites by calling
create_website_button() with new URLs.
Example Usage:
- Click "YouTube" to see its QR code.
- Enter
https://example.com in the custom field and click "Generate Custom".
This will give you a functional QR code generator with error handling and a user-friendly interface. You can expand it further by adding features like saving QR codes to files or adjusting QR code parameters.