Python QRCode Reader - Generate QRCode and Read QRcode Using Tkinter
Published February 03, 2022QR code is a collection of square barcodes used to encode data on optical labels. They store data using the pattern of the white spaces and black dots in a square grid. QR codes can store any type of data, such as numerical or simple text, passwords information, etc.
In this post, we will learn how to generate a QR code, store information in it, and read it using Python Tkinter.
How to Generate and Read QR Code using Python Tkinter
The following steps will show you how to read a QR code using Python:
Step 1: Install all the required libraries in our python application. To install these libraries, open your Terminal and run the following commands
Pip install pypng Pip install pyzbar pip install pillow Pip install pyqrcode |
Once all the libraries have been installed, we will import them into our application.
Step 2: Add the following code to import all the above-installed libraries
from tkinter import * from tkinter import ttk from tkinter import filedialog from PIL import Image, ImageTk from pyzbar.pyzbar import decode import pyqrcode import os |
Having imported the required libraries, the next step is to create a GUI for our application.
Step 3: Add the following code to create the GUI user interface for the application
root = Tk() root.title("Pytyhon QR Code Generate and Read") note = ttk.Notebook(root) note.pack() create frames to add on tabs frame1 = Frame(note, height=400, width=150, bg='white') frame1.pack(fill="both", expand=True) frame2 = Frame(note, height=400, width=150, bg='white') frame2.pack(fill="both", expand=True) s = ttk.Style() s.theme_create("style", parent="alt", settings={ "TNotebook.Tab": {"configure": {"padding": [20, 10], "font": ('Arial', '12', 'bold')}}}) s.theme_use("style") # add tabs note.add(frame1, text="Generate QR Code") note.add(frame2, text="Read QR Code") create canvas to display QR Code image canvas1 = Canvas(frame1, width="400", height="300", relief=RIDGE, bd=2) canvas1.pack(padx=10, pady=10) canvas2 = Canvas(frame2, width="400", height="400", relief=RIDGE, bd=2) canvas2.pack(padx=10, pady=10) data_label = Label(frame1, text='QR Code Data:', font=('ariel 11 bold'), bg='white') data_label.place(x=61, y=330) save_label = Label(frame1, text='QR code name :', font=('ariel 11 bold'), bg='white') save_label.place(x=55, y=360) data_entry = Entry(frame1, font=('ariel 11 bold'), relief=GROOVE, bd=3) data_entry.place(x=197, y=330) save_entry = Entry(frame1, font=('ariel 11 bold'), relief=GROOVE, bd=3) save_entry.place(x=197, y=380) btn1 = Button(frame1, text="Generate", bg='gray', fg='black', font=('ariel 11 bold'), relief=GROOVE, command=generate) btn1.place(x=85, y=425) btn2 = Button(frame1, text="Close", width=10, bg='gray', fg='black', font=('ariel 11 bold'), relief=GROOVE, command=root.destroy) btn2.place(x=255, y=425) btn2 = Button(frame2, text="Select QR Code Image", bg='gray', fg='black', font=('ariel 11 bold'), relief=GROOVE, command=selected) btn2.pack(side=LEFT, padx=50, pady=5) btn3 = Button(frame2, text="Exit", width=12, bg='gray', fg='black', font=('ariel 11 bold'), relief=GROOVE, command=root.destroy) btn3.pack(side=LEFT, padx=10, pady=5) root.mainloop() |
However, now that the user interface has been created, the application cannot perform any of its functions, such as reading or generating QR Codes. As a result, we need to create functions to enable our buttons to work.
-
Step 4: Write functions for generating QR codes and reading QR codes.
First, create the QR code generate function by pasting the following code below
def generate(): if data_entry.get() != '' and save_entry.get() != '': qr = pyqrcode.create(data_entry.get()) img = qr.png(save_entry.get() + ".png", scale=5) info = Label(frame1, text="Generated QR code:", font=('ariel 15 bold')) info.place(x=60, y=40) img = Image.open(save_entry.get() + ".png") img = ImageTk.PhotoImage(img) canvas1.create_image(200, 180, image=img) canvas1.image = img else: info = Label(frame1, text="Please enter the data for QR code", font=('Times 15 bold')) info.place(x=80, y=140) |
Secondly, create a function to read QR codes. To do this, paste the code below
def selected(): img_path = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select QR Code", filetype=( ("PNG file", "*.png"), ("All files", "*.*"))) img = Image.open(img_path) img = ImageTk.PhotoImage(img) canvas2.create_image(200, 190, image=img) canvas2.image = img d = decode(Image.open(img_path)) data = d[0].data.decode() qrcode_data = Label(frame2, text=data, bg='gold', fg='black', font=('ariel 15 bold'), relief=GROOVE) qrcode_data.place(x=150, y=380) |
Step 5: Run your application now. Everything is working perfectly
Conclusion: In this python example we covered how to generate QR code with python and QR Code reader with python
Article Contributed By :
|
|
|
|
722 Views |