Python QR Code Generator Project with Source Code

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Nowadays we all use QR code for multiple purposes like making payments, sharing WiFi, etc. Have you ever wished to generate such QR code on your own? We are here with a project for QR code generator using Python. So, follow us to build this Python project.

About QR Code Generator Project

QR Code is a machine-readable matrix barcode that uniquely represents information. With the increase in optical capabilities of smartphones, the use of QR codes started increasing.

In this project, we will build a QR Code generator using Python Modules.

Generate QR Code in Python

We will be using Tkinter and Qrcode modules in Python to build this project. We use the Tkinter module to build the GUI to take the following inputs:

  1. text/URL to convert to QR Code
  2. location to save the QR code with name
  3. size of the QR code.

Then we generate the QR code from the above inputs and save in the given location using the qrcode module.

Download the Source Code for QR Code Generator in Python

Please download the source code for the QR code generator in Python using the link: Python QR Code Generator Project

Project Prerequisites

It is suggested that the developer has prior knowledge on Python and the Tkinter module. Also, use the below commands to install the qrcode and tkinter modules.

pip install tkinter
pip install qrcode

Steps to build the QR Code Generator in Python

To build the QR code generator project using Python we need to follow the below steps:
1. Importing the modules
2. Creating the main window
3. Taking the input of the text/URL, location to store the QR code, name of the QR code and the size of the QR code
4. Writing the function to generate and save the QR Code

1. Importing the modules

The first step is to import the qrcode and the tkinter module. We use the messagebox in the tkinter module to show the pop up messages.

import qrcode
from tkinter import *
from tkinter import messagebox

2. Creating the main window

Next, we create the main window with title, size and color.

#Creating the window
wn = Tk()
wn.title('DataFlair QR Code Generator')
wn.geometry('700x700')
wn.config(bg='SteelBlue3')

3. Taking the inputs

Now, we take the inputs from the user to create the QR Code. We take the following inputs:

1. Text/URL as Entry() named as ‘text’

2. Location to save the QR Code as Entry() named as ‘loc’

3. Name of the QR Code image when saved in the device as Entry() named as ‘name’

4. Size of the QR Code to be generated as Entry() named ‘size’. In this the user has to give the size in the range 1-40. 1 being the smallest size of 21×21.

Then we create a button when clicked generates the QR Code and saves it by executing the generateCode() function.

#Label for the window
headingFrame = Frame(wn,bg="azure",bd=5)
headingFrame.place(relx=0.15,rely=0.05,relwidth=0.7,relheight=0.1)
headingLabel = Label(headingFrame, text="Generate QR Code with DataFlair", bg='azure', font=('Times',20,'bold'))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

#Taking the input of the text or URL to get QR code
Frame1 = Frame(wn,bg="SteelBlue3")
Frame1.place(relx=0.1,rely=0.15,relwidth=0.7,relheight=0.3)

label1 = Label(Frame1,text="Enter the text/URL: ",bg="SteelBlue3",fg='azure',font=('Courier',13,'bold'))
label1.place(relx=0.05,rely=0.2, relheight=0.08)

text = Entry(Frame1,font=('Century 12'))
text.place(relx=0.05,rely=0.4, relwidth=1, relheight=0.2)

#Getting input of the location to save QR Code
Frame2 = Frame(wn,bg="SteelBlue3")
Frame2.place(relx=0.1,rely=0.35,relwidth=0.7,relheight=0.3)

label2 = Label(Frame2,text="Enter the location to save the QR Code: ",bg="SteelBlue3",fg='azure',font=('Courier',13,'bold'))
label2.place(relx=0.05,rely=0.2, relheight=0.08)

loc = Entry(Frame2,font=('Century 12'))
loc.place(relx=0.05,rely=0.4, relwidth=1, relheight=0.2)

#Getting input of the QR Code image name
Frame3 = Frame(wn,bg="SteelBlue3")
Frame3.place(relx=0.1,rely=0.55,relwidth=0.7,relheight=0.3)

label3 = Label(Frame3,text="Enter the name of the QR Code: ",bg="SteelBlue3",fg='azure',font=('Courier',13,'bold'))
label3.place(relx=0.05,rely=0.2, relheight=0.08)

name = Entry(Frame3,font=('Century 12'))
name.place(relx=0.05,rely=0.4, relwidth=1, relheight=0.2)

#Getting the input of the size of the QR Code
Frame4 = Frame(wn,bg="SteelBlue3")
Frame4.place(relx=0.1,rely=0.75,relwidth=0.7,relheight=0.2)

label4 = Label(Frame4,text="Enter the size from 1 to 40 with 1 being 21x21: ",bg="SteelBlue3",fg='azure',font=('Courier',13,'bold'))
label4.place(relx=0.05,rely=0.2, relheight=0.08)

size = Entry(Frame4,font=('Century 12'))
size.place(relx=0.05,rely=0.4, relwidth=0.5, relheight=0.2)

#Button to generate and save the QR Code
button = Button(wn, text='Generate Code',font=('Courier',15,'normal'),command=generateCode)
button.place(relx=0.35,rely=0.9, relwidth=0.25, relheight=0.05)

#Runs the window till it is closed manually
wn.mainloop()

4. Creating the function to generate QR code and save it

Finally, we create the function to generate the code that runs on clicking the button. In this,

1. First we create the QRCode object with the version/size that user gave as input in the size() entry

2. Then we add the text that we need to encode by getting from the entry ‘text’

3. Then we get the QR code and save it in the directory that user gave as input

4. After this, we show the pop up message to confirm the user that the image is saved

#Function to generate the QR code and save it
def generateCode():
  #Creating a QRCode object of the size specified by the user
  qr = qrcode.QRCode(version = size.get(),
            box_size = 10,
            border = 5)
  qr.add_data(text.get()) #Adding the data to be encoded to the QRCode object
  qr.make(fit = True) #Making the entire QR Code space utilized
  img = qr.make_image() #Generating the QR Code
  fileDirec=loc.get()+'\\'+name.get() #Getting the directory where the file has to be save
  img.save(f'{fileDirec}.png') #Saving the QR Code
  #Showing the pop up message on saving the file
  messagebox.showinfo("DataFlair QR Code Generator","QR Code is saved successfully!")

Output of the QR Code Generator in Python

Image of the GUI to take inputs from the user for generating QR Code

python qr code generator output

Summary

Hurray! We have successfully built the QR Code generator in Python. In this, we learned the basic widgets of the tkinter module and to generate the QR Code using the qrcode module. Hope you enjoyed building this project.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

15 Responses

  1. shoaib ahmad says:

    which software do we need to make this project

  2. Gayathri says:

    Hello Shoaib Ahmad! You can use any of the Python IDEs like IDLE, PyCharm, Visual Studio, Spyder, etc. to install required modules and write and run the code. Hope I could answer your question!

  3. Yogi says:

    When I am trying to run the code its showing a syntax error command=generateCode is not defined.

  4. Akash says:

    you have to create function generateCode()
    then when you click on button then generateCode function will be called

  5. Goutam Badaghara says:

    some error is coming how to solve it
    the error is –
    Exception in Tkinter callback
    Traceback (most recent call last):
    File “D:\python\Lib\site-packages\qrcode\image\pil.py”, line 5, in
    from PIL import Image, ImageDraw
    ModuleNotFoundError: No module named ‘PIL’

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File “D:\python\Lib\tkinter\__init__.py”, line 1948, in __call__
    return self.func(*args)
    ^^^^^^^^^^^^^^^^
    File “c:\Users\badag\OneDrive\Desktop\QR_CODE\qr-code-generator\qrCode.py”, line 20, in generateCode
    img = qr.make_image() #Generating the QR Code
    ^^^^^^^^^^^^^^^
    File “D:\python\Lib\site-packages\qrcode\main.py”, line 296, in make_image
    from qrcode.image.pil import PilImage
    File “D:\python\Lib\site-packages\qrcode\image\pil.py”, line 7, in
    import Image
    ModuleNotFoundError: No module named ‘Image’

    • DataFlair Team says:

      This error mainly occurs, when you do not have the module you tried importing installed on your IDE.Here are the commands to import the required modules from the cmd(command prompt)
      .
      PIL (Pillow): pip install Pillow.
      IMAGE : pip install image.

  6. Aashish Chaudhary says:

    Hello, I think we need some correction on line 48..

  7. deepak says:

    AttributeError: module ‘qrcode’ has no attribute ‘QRCode’

    solve it

Leave a Reply

Your email address will not be published. Required fields are marked *