Have you ever wanted to generate a QR code for your website, contact information, or a secret message?
In this tutorial, I’ll show you how to build a Python program that generates QR codes with ease. By the end, you’ll have a simple application with a graphical user interface (GUI) that allows users to input a URL and generate a QR code in just a few clicks.
This project is beginner-friendly and demonstrates how to use Python libraries for image processing and GUI development. Let’s dive in!
How To Build a Python QR Code Generator
A QR code generator is a fun and practical Python project to enhance your skills. Here’s what you’ll learn:
- Using the qrcode library to generate QR codes.
- Leveraging tkinter to create a graphical user interface.
- Working with Pillow (PIL) to handle and display images.
This project is not only educational but also highly useful in real-world applications. Let’s get started to build the app I've shown below:
Project Prerequisites
Before we begin coding, let’s review the skills and tools needed for this tutorial.
Don’t worry if you’re not a Python expert just yet! Having a few basics under your belt will make this journey smoother and more enjoyable.
Basic Python Knowledge
You should be familiar with:
- Variables, functions, and loops.
- Basic file handling and exception handling.
Required Libraries
I’ll be using the following Python libraries:
- qrcode for generating QR codes.
- tkinter for creating the GUI.
- Pillow (PIL) for displaying the generated QR code.
If you don't have all of these yet, you can easily install them with this command:
pip install qrcode pillow
A Curious and Experimental Mind
For me, a willingness to experiment and make small adjustments are some of the keys to learning. I want you to be ready to explore and debug as you go along!
Step 1: Set Up Your Project
Before I jump into coding, let’s set up the project:
1. Make sure Python is installed on your computer. If not, download it from the official Python website.
2. Open your favorite code editor or IDE.
3. Create a new Python file, for example, qr_code_generator.py.
Great, now, let's dive head first into our Python editor to get this build started.
Step 2: Import Required Libraries
Let’s start by importing the libraries we’ll use:
import tkinter as tk from tkinter import ttk, messagebox import qrcode from PIL import Image, ImageTk
Step 3: Build the GUI
Initialize the Main Window
We'll be using tkinter to create a GUI, so begin by setting up the main application window:
# Create the main window root = tk.Tk() root.title("QR Code Generator") root.geometry("400x500") root.configure(bg="#f0f4f7")
Add Input Fields and Generate Button
We’ll add an entry box for the URL and a button to generate the QR code:
# URL input label_url = ttk.Label(root, text="Enter URL:", background="#f0f4f7") label_url.pack(pady=10) entry_url = ttk.Entry(root, font=("Helvetica", 12), width=40) entry_url.pack(pady=10) # Generate button button_generate = ttk.Button(root, text="Generate QR Code", command=None) button_generate.pack(pady=20)
Add a Label to Display the QR Code
Create a label to display the generated QR code:
# Label to display the QR code qr_label = ttk.Label(root, background="#f0f4f7") qr_label.pack(pady=10)
Step 4: Write the QR Code Generation Function
Here’s the function to generate and display the QR code:
def generate_qrcode(): url = entry_url.get() if not url: messagebox.showerror("Error", "Please enter a valid URL.") return # Generate the QR code qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4, ) qr.add_data(url) qr.make(fit=True) # Create and save the QR code image qr_image = qr.make_image(fill_color="black", back_color="white") qr_image.save("qrcode.png") # Display the QR code qr_image_tk = ImageTk.PhotoImage(Image.open("qrcode.png")) qr_label.config(image=qr_image_tk) qr_label.image = qr_image_tk messagebox.showinfo("Success", "QR Code generated successfully!")
Link the Button to the Function
Connect the generate_qrcode function to the button:
button_generate.config(command=generate_qrcode)
Step 5: Run the Application
Start the tkinter event loop to launch the application:
root.mainloop()
Full Program Source Code
Here’s the complete code for the Python QR Code Generator:
''' Hackr.io Python Tutorial: QR Code Generator ''' import tkinter as tk from tkinter import ttk from tkinter import messagebox import qrcode from PIL import Image, ImageTk def generate_qrcode(): url = entry_url.get() if not url: messagebox.showerror("Error", "Please enter a valid URL.") return # Generate the QR code qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=5, border=2, ) qr.add_data(url) qr.make(fit=True) # Create an image of the QR code qr_image = qr.make_image(fill_color="black", back_color="white") qr_image.save("qrcode.png") # Display the QR code in the application qr_image_tk = ImageTk.PhotoImage(Image.open("qrcode.png")) qr_label.config(image=qr_image_tk) qr_label.image = qr_image_tk label_result.config(text="QR Code generated successfully!") # Create the main window root = tk.Tk() root.title("QR Code Generator") root.geometry("400x500") root.configure(bg="#f0f4f7") # URL input label_url = ttk.Label(root, text="Enter URL:", background="#f0f4f7") label_url.pack(pady=10) entry_url = ttk.Entry(root, font=("Helvetica", 12), width=40) entry_url.pack(pady=10) # Generate button button_generate = ttk.Button( root, text="Generate QR Code", command=generate_qrcode, style="TButton") button_generate.pack(pady=20) # Label to display the QR code qr_label = ttk.Label(root, background="#f0f4f7") qr_label.pack(pady=10) # Result label label_result = ttk.Label(root, text="", font=( "Helvetica", 14), background="#f0f4f7", foreground="#333") label_result.pack(pady=20) # Styling style = ttk.Style() style.configure("TButton", font=("Helvetica", 12), padding=5) style.map("TButton", background=[("active", "#0056b3"), ("!active", "#007bff")], foreground=[("active", "white"), ("!active", "white")]) # Start the Tkinter event loop root.mainloop()
Video Walkthrough
Wrapping Up
You’ve just built a Python QR code generator, that's impressive! Hopefully, this project has shown you how Python can leverage libraries like qrcode and tkinter to create practical applications.
Feel free to expand this project by:
- Adding more customization options, like changing QR code colors.
- Allowing users to save the QR code to a custom location.
- Supporting other types of input, like text or contact details.
Happy coding!