Introduction
Today, we will learn how to Generate QR Code in Python. QR stands for Quick Response, and it gained its name from how quickly a cell phone can read it. They are used on your phone to collect and store data from moving media. This tutorial will show you how to make QR codes with Python.
How to Generate QR Code in Python?
To generate QR codes, we’ll use the Python package QRcode and Tkinter for the GUI application. Here is the pip command to use to install this package:
pip install qrcode
&
pip install tk
Now, let’s look at how to make a QRCode using the Python programming language.
Step 1: Importing Qrcode and Tkinter libraries
import qrcode
from tkinter import *
Step 2: Making the primary window
cp = Tk()
cp.title('violet-cat-415996.hostingersite.com')
cp.geometry('700x250')
cp.config(bg='#e52165')
In this phase, we’ll create the main window with the title, size, and color. Where we set the title to [violet-cat-415996.hostingersite.com], the size of the window will be 700×250, and the background color will be ‘#e52165’.
Step 3: Create a function that takes any text or URL as an input and generates a QR code
def generate():
img = qrcode.make(msg.get())
type(img)
img.save(f'{save_name.get()}.png')
Label(cp, text='File Saved!', bg='#e52165', fg='black', font=('Arial Black', 8)).pack()
def show():
img = qrcode.make(msg.get())
type(img)
img.show()
We must import the QRcode module. Then we use the qrcode.make() function, and pass the string as a parameter. This string will aid in the generation of the QR code using the procedure. The full QR construction will be saved in the image object. Then we’ll create an IMG object and use img.save() to store it. When a picture is saved, the label function is used to display a popup(‘File Saved’), Where save_name fetch from ‘File Name(Save As)’ Entry Frame, where the show() function is used to show the QR code.
Step 4: Create a User Interface for the application to Generate QR Code in Python
frame = Frame(cp, bg='#e52165')
frame.pack(expand=True)
The Python Tkinter serves as a container for the other components. The rectangular sections of the screen are utilized to organize the Python application’s components.
#------------------ENTER THE TEXT OR URL------------------
Label(frame, text='Enter the Text or URL : ', font=('Arial Black', 16),
bg='#e52165').grid(row=0, column=0, sticky='w')
msg = Entry(frame)
msg.grid(row=0, column=1)
To begin, we must establish an input box to receive the text entered by the user. To do so, we built Entry(frame), which is saved in (msg), and we generated a label for it (‘Enter the Text or URL’). We used the grid() function to align the input box, label, and buttons.
#------------------ENTER THE FILE NAME------------------
Label(frame, text='File Name(Save As) : ', font=('Arial Black', 16),
bg='#e52165').grid(row=1, column=0, sticky='w')
save_name = Entry(frame)
save_name.grid(row=1, column=1)
Following that, we create another input text box for naming the QRcode file. To do this, we established an Entry frame and its object ‘save_name’.
#------------------BUTTONS TO SHOW OR SAVE QRCODE------------------
btn = Button(cp, text='Show QR code', bd='5', command=show, width=15)
btn.pack()
btn = Button(cp, text='Save QR code', command=generate, bd='5', width=15)
btn.pack()
There are two buttons that we designed. The first is used to view the QR Code; when pressed, it will redirect to the show() method that we built before, and the second is used to store the QR Code; when pressed, it will redirect to the generate() function.
Complete Code to Generate QR Code in Python
import qrcode from tkinter import * cp = Tk() cp.title('violet-cat-415996.hostingersite.com') cp.geometry('700x250') cp.config(bg='#e52165') def generate(): img = qrcode.make(msg.get()) type(img) img.save(f'{save_name.get()}.png') Label(cp, text='File Saved!', bg='#e52165' , fg='black', font=('Arial Black', 8)).pack() def show(): img = qrcode.make(msg.get()) type(img) img.show() frame = Frame(cp, bg='#e52165') frame.pack(expand=True) #------------------ENTER THE TEXT OR URL------------------ Label(frame, text='Enter the Text or URL : ', font=('Arial Black', 16), bg='#e52165').grid(row=0, column=0, sticky='w') msg = Entry(frame) msg.grid(row=0, column=1) #------------------ENTER THE FILE NAME------------------ Label(frame, text='File Name(Save As) : ', font=('Arial Black', 16), bg='#e52165').grid(row=1, column=0, sticky='w') save_name = Entry(frame) save_name.grid(row=1, column=1) #------------------BUTTONS TO SHOW OR SAVE QRCODE------------------ btn = Button(cp, text='Show QR code', bd='5', command=show, width=15) btn.pack() btn = Button(cp, text='Save QR code', command=generate, bd='5', width=15) btn.pack() cp.mainloop()
Output
In Python, we successfully developed the QR Code generator. This tutorial taught us how to use the Tkinter module’s basic widgets and how to Generate QR Code in Python with the qrcode module. I hope you have fun working on this project.
We hope this article will help you to learn how to Generate QR Code in Python.
Also Read:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Music Recommendation System in Machine Learning
- Create your own ChatGPT with Python
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- 100+ Java Projects for Beginners 2023
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Courier Tracking System in HTML CSS and JS
- Test Typing Speed using Python App