QR or Quick Response codes are a type of two-dimensional barcode that can store information ranging from URLs to contact details, making them an incredibly versatile tool for digital information sharing.
In today’s tutorial, we are going to create a QR code generator using the tkinter
library. All the user has to do is input a title and a link to a video, article, image, or basically anything, and then click the button. A QR code that leads to the provided link will appear when scanned.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- QR Code Function
- Creating the Main Window
- Creating GUI Elements
- Display the QR Code Image
- Main Loop
- Example
- Full Code
Necessary Libraries
$ pip install tk
$ pip install qrcode
Those are the required libraries that you should install so the code functions properly.
Imports
import tkinter as tk
import qrcode
We start by importing two libraries. The first one is tkinter, which allows us to create a graphical user interface (GUI). The second one is qrcode which allows us to generate QR codes.
QR Code Function
def generate_qr_code():
title = title_entry.get()
text = text_entry.get()
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(f"Qrcode/{title}.png") #Qrcode is a folder created in the same folder this code is saved
global qr_image # Declare qr_image as a global variable
qr_image = tk.PhotoImage(file=f"Qrcode/{title}.png") # Store PhotoImage as a global variable
qr_image_label.config(image=qr_image) # Update the Label with the PhotoImage
The code presented here creates a function that retrieves the title and the link from the entry fields to generate a QR code image with the link as a parameter and then saves the generated image in a filename based on the title inputted by the user, then the saved Qr code image is loaded in the main window.
Creating the Main Window
root = tk.Tk()
root.title("QR Code Generator - The Pycodes")
root.geometry("800x500")
root.config(bg="black")
Now we create the main window, give it a title, and define its geometry and background color.
Creating GUI Elements
title_label = tk.Label(root, text="Title:",font="arial 12 bold",bg="black",fg="white")
title_label.place(x=50,y=100)
title_entry = tk.Entry(root,width=30,font="arial 12")
title_entry.place(x=100,y=100)
text_label = tk.Label(root, text="Link:",font="arial 12 bold",bg="black",fg="white")
text_label.place(x=50,y=160)
text_entry = tk.Entry(root,width=30,font="arial 12")
text_entry.place(x=100,y=160)
generate_button = tk.Button(root, text="Generate QR Code", command=generate_qr_code)
generate_button.place(x=170,y=210)
Next, we create the title label and its entry field, the text (link/URL) label and its entry field, and finally, the ‘Generate QR Code‘ button that triggers the generate_qr_code
function.
Display the QR Code Image
qr_image_label = tk.Label(root,bg="black")
qr_image_label.place(x=400,y=50)
Here, we created a label widget that will display the generated QR Code image in the main window.
Main Loop
root.mainloop()
Lastly, this part ensures that the main window keeps running and responsive until the user exits.
Example
Full Code
import tkinter as tk
import qrcode
def generate_qr_code():
title = title_entry.get()
text = text_entry.get()
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(f"Qrcode/{title}.png") #Qrcode is a folder created in the same folder this code is saved
global qr_image # Declare qr_image as a global variable
qr_image = tk.PhotoImage(file=f"Qrcode/{title}.png") # Store PhotoImage as a global variable
qr_image_label.config(image=qr_image) # Update the Label with the PhotoImage
root = tk.Tk()
root.title("QR Code Generator - The Pycodes")
root.geometry("800x500")
root.config(bg="black")
title_label = tk.Label(root, text="Title:",font="arial 12 bold",bg="black",fg="white")
title_label.place(x=50,y=100)
title_entry = tk.Entry(root,width=30,font="arial 12")
title_entry.place(x=100,y=100)
text_label = tk.Label(root, text="Link:",font="arial 12 bold",bg="black",fg="white")
text_label.place(x=50,y=160)
text_entry = tk.Entry(root,width=30,font="arial 12")
text_entry.place(x=100,y=160)
generate_button = tk.Button(root, text="Generate QR Code", command=generate_qr_code)
generate_button.place(x=170,y=210)
qr_image_label = tk.Label(root,bg="black")
qr_image_label.place(x=400,y=50)
root.mainloop()
Happy Coding!