QR Code Generator with GUI in Python Using (qrcode, PIL, and tkinter)

Overview

We will create a Python program that generates a QR code from a user-provided link and saves it as a PNG image file. The program uses the "qrcode" and "PIL" libraries for generating and saving the QR code respectively. It also uses the "tkinter" library to create a Graphical User Interface (GUI) window that allows the user to input the link and image name, and then generate the QR code by clicking on the "Create QR Code" button. 

Copy the Full Code ↓ or Download it From GitHub

Understand Code

import qrcode
from PIL import Image
import tkinter as tk

Import the required libraries: qrcode for generating QR codes, PIL for saving images, and tkinter for creating a GUI window.


def create_qr():
    link = link_entry.get()
    img_name = img_name_entry.get()
    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=20, border=4)
    qr.add_data(link)
    qr.make(fit=True)
    img = qr.make_image()
    img.save(f'{img_name}.png')
    status_label.config(text="QR code created successfully.")

Define the create_qr() function that creates the QR code image. It retrieves the link and image name from the corresponding input fields in the GUI window. It creates a QRCode object with the specified parameters, adds the link to the object, and generates the QR code image using the make_image() method. It saves the image as a PNG file with the specified name. It also updates a status label in the GUI window to inform the user of the success of the operation.


root = tk.Tk()
root.title("QR Code Generator")

Create the main window using the tkinter.Tk() method, and set its title to "QR Code Generator".


link_label = tk.Label(root, text="Enter the link:")
link_label.pack()
link_entry = tk.Entry(root)
link_entry.pack()

Create a label and an entry field for the link, and pack them vertically in the window using the tkinter.Label() and tkinter.Entry() methods, respectively.


img_name_label = tk.Label(root, text="Enter the image name:")
img_name_label.pack()
img_name_entry = tk.Entry(root)
img_name_entry.pack()

Create a label and an entry field for the image name, and pack them vertically in the window.


create_button = tk.Button(root, text="Create QR code", command=create_qr)
create_button.pack()

Create a button that triggers the create_qr() function when clicked, and pack it in the window.


status_label = tk.Label(root, text="")
status_label.pack()

Create a label for the status message and pack it in the window.


root.mainloop()

Start the main loop of the program using the mainloop() method, which waits for user input and responds to events such as button clicks.

Copy the Full Code

import qrcode
from PIL import Image
import tkinter as tk

def create_qr():
    link = link_entry.get()
    img_name = img_name_entry.get()
    qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=20, border=4)
    qr.add_data(link)
    qr.make(fit=True)
    img = qr.make_image()
    img.save(f'{img_name}.png')
    status_label.config(text="QR code created successfully.")

# create the main window
root = tk.Tk()
root.title("QR Code Generator")

# create input field for link
link_label = tk.Label(root, text="Enter the link:")
link_label.pack()
link_entry = tk.Entry(root)
link_entry.pack()

# create input field for image name
img_name_label = tk.Label(root, text="Enter the image name:")
img_name_label.pack()
img_name_entry = tk.Entry(root)
img_name_entry.pack()

# create button to generate QR code
create_button = tk.Button(root, text="Create QR code", command=create_qr)
create_button.pack()

# create label to show status
status_label = tk.Label(root, text="")
status_label.pack()

# start the main loop
root.mainloop()

Post a Comment

0 Comments

© Copyright 2024 & 2025 - Team Krope - All right reserved