Generate QR Code in Python

Generate QR Code in Python

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('copyassignment.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 [copyassignment.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('copyassignment.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

Output for GUI App to Generate QR Code in Python
Output to Generate QR Code in Python

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:

Share:
Avatar of Vatsal Rakholiya

Author: Vatsal Rakholiya