In this article, we will guide you through the process of generating a QR code from a given text using Python, specifically utilizing the Tkinter module for the graphical interface. QR codes are a convenient way to encode information, such as URLs or plain text, into a format that can be easily scanned by smartphones or other devices.
Step1:
Before starting with the coding, you need to install the necessary Python packages. Tkinter is the standard GUI (Graphical User Interface) library in Python, and it’s usually included with Python installations. However, you need to install the pyqrcode
and pypng
packages to generate and save QR codes. Use the following commands:
- Tkinter
- pyqrcode
- pypng
pip3 install pyqrcode pip3 install pypng
Step 2:
Begin by importing the required modules (tkinter
for the GUI and pyqrcode
for QR code generation). Create a Tkinter window with a title and set its size. Within this window, the user can enter text, which will be converted into a QR code when they click the “Convert” button. The code uses the create()
function from pyqrcode
to generate the QR code and saves it as a PNG file. The generated QR code is then displayed in the Tkinter window using the PhotoImage
class.
import tkinter as tk import pyqrcode as pq win=tk.Tk() win.title("Text To Qr Generator") win.geometry("600x400+600+150") def textToQr(): st=lab1.get() qr=pq.create(st) qr.png('QrCode.png',scale=6) qr_img=tk.PhotoImage(file='QrCode.png') lab2.config(image=qr_img) lab2.image=qr_img lab1=tk.Entry(win,width=50) lab1.pack(pady=10) lab2=tk.Label(win) lab2.pack() but=tk.Button(win,text="Convert",command=textToQr) but.pack() win.mainloop()
Output:
When you run the code and enter text, a QR code image corresponding to that text will be displayed in the window like below.