QR Code Generator – A Simple and Interactive Project
Introduction
The QR Code Generator is a beginner-friendly yet practical Python project
that allows users to create QR codes instantly. QR codes are widely used for
storing URLs, contact information, and other data in a scannable format. With
this project, users can enter any text or link, click a button, and generate a
QR code right inside a graphical user interface (GUI).
This project utilizes Python libraries such as:
Tkinter – To build the GUI.
Qrcode – To generate QR codes.
PIL (Pillow) – To handle and display images.
Ttkbootstrap – To enhance the look of the interface.
It’s an excellent way to learn about Python GUI development while creating
something useful.
Setting Up the Interface
The first step in building this project is creating the main application window.
This is done using tkinter, which provides the structure for the interface:
Root = tk.Tk()
Root.title(“QR Code Generator”)
Root.geometry(“500x500”)
This code:
Initializes the main window.
Sets the window title.
Defines a fixed size (500x500 pixels).
To make the interface look more modern, we use ttkbootstrap:
Style = Style(theme=’flatly’)
Style.theme_use()
This applies the “flatly” theme, giving buttons and input fields a clean,
professional look
Adding Input and Button
Users need a way to enter text and generate the QR code. This is achieved
by adding an input field and a button:
Text_label = ttk.Label(master=root, text=”Enter Text or URL:”)
Text_label.pack(pady=10)
Text_entry = ttk.Entry(master=root, width=50)
Text_entry.pack()
The label tells users what to enter.
The entry field allows users to type in their text or URL.
The Generate QR Code button triggers the creation of the QR code:
Generate_button = ttk.Button(master=root, text=”Generate QR Code”,
Command=generate_qr_code, style=’success.TButton’)
Generate_button.pack(pady=10)
When clicked, it calls the generate_qr_code() function.
The “success.TButton” style makes the button look visually appealing.
Generating the QR Code
The generate_qr_code() function handles the QR code creation and display:
Def generate_qr_code():
# Get user input
Text = text_entry.get()
# Create a QR code
Qr = qrcode.QRCode(version=1, box_size=10, border=5)
Qr.add_data(text)
Qr.make(fit=True)
Img = qr.make_image(fill_color=”black”, back_color=”white”)
# Display the QR Code in the GUI
Img = img.resize((300,300))
Img_tk = ImageTk.PhotoImage(img)
Qr_label.configure(image=img_tk)
Qr_label.image = img_tk
How It Works
1. Retrieves user input from the text field.
2. Creates a QR code with
Version=1: Defines the size.
Box_size=10: Sets the size of each QR code module.
Border=5: Adds a margin around the QR code.
3. Generates the QR code and converts it into an image.
4. Displays the QR code inside the application window.
A label is used to display the generated QR code:
Qr_label = ttk.Label(master=root)
Qr_label.pack()
Initially, this label is empty, but once the user generates a QR code, it
updates with the new image.
Running the Application
The final step is to ensure the application keeps running and responding to
user interactions:
Root.mainloop()
This starts the main event loop, allowing the program to handle button clicks
and display updates.
Why This Project is Useful
Quick and Easy – Users can generate QR codes instantly.
No Coding Needed for Users – The interface makes it simple for anyone to
use.
Customizable – The QR code can be styled with different colors and sizes.
Practical Application – QR codes are widely used for links, payments, and
more.
Final Thoughts
The QR Code Generator is a great beginner project that combines Python’s
GUI capabilities with image processing. It’s practical, fun to use, and serves
as a solid introduction to working with graphical interfaces in Python.
Whether you’re looking to build a simple tool or expand it into something
more advanced, this project is an excellent starting point!
Input:
import qrcode
import tkinter as tk
from PIL import ImageTk, Image
from ttkbootstrap import Style
import tkinter.ttk as ttk
root = tk.Tk()
root.title("QR Code Generator")
root.geometry("500x500")
style = Style(theme='flatly')
style.theme_use()
def generate_qr_code():
# get the text entered by the user
text = text_entry.get()
# create a QR code from the text
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
# display the QR Code in the GUI
img = img.resize((300,300))
img_tk = ImageTk.PhotoImage(img)
qr_label.configure(image=img_tk)
qr_label.image = img_tk
# create a text input field for the text
text_label = ttk.Label(master=root, text="Enter Text or URL:")
text_label.pack(pady=10)
text_entry = ttk.Entry(master=root, width=50)
text_entry.pack()
# create a button to generate the QR Code
generate_button = ttk.Button(master=root, text="Generate QR Code",
command=generate_qr_code, style='success.TButton')
generate_button.pack(pady=10)
# create a label to display the QR Code
qr_label = ttk.Label(master=root)
qr_label.pack()
root.mainloop()
Output: