Generate QR Code using Python

FREE Online Courses: Enroll Now, Thank us Later!

Python QR Code Generator is an application that creates QR Code for a particular string or URL. In this application, the user will have an entry field to enter the url or the string and a QR Code will be generated accordingly and will be saved in the system. Let’s develop QR Code Generator Project using Python.

About QR Code Generator

QR Code Generator is an application that takes in a url or a string and creates a qr code for it. Here we can save the generated QR code as an image with the .png extension.

Python QR Code Generator Project Details

In this project, we are going to create a QR Code Generator using Python. For creating the GUI of the project we are going to use the Tkinter Module and its inbuilt function. For creating a QR Code we are going to use the pyqrcode Library.
We will be creating a GUI Window that will have an entry field to enter the string or the url and the QR Code will be generated for this entered string.

Project Prerequisites

We will have to install the following libraries for proceeding with the project:

  • Tkinter Module – We will be using the Tkinter Module to create the GUI of our project. Use the following command to install it.
pip install tkinter
  • Png Module – Png Module will help to save the QR Code as an image. Use the following command to install it.
pip install pypng
  • Pyqrcode Module – This module will help us generate the QR Code. Use the following command to install it.
pip install pyqrcode

Download Python QR Code Generator Project Code

Please download the source code of QR Code Generator Project from the following link: Python QR Code Generator Code

Steps to Develop QR Code Generator Project using Python

Following are the steps to create the Python QR Code Generator Project.

  1. Importing the Required Libraries and Modules
  2. Creating the GUI Window
  3. Creating the QR Code Generator function
  4. Create Button and Entry Field
  5. We will be looking at each step in detail

1. Importing the Required Libraries and Modules:

import pyqrcode
import png
from pyqrcode import QRCode
import tkinter as tk
from tkinter import *
  • Tkinter Module – Tkinter Module helps to create the GUI window for our project.
  • Png Module – This helps to save an image in the png extension.
  • Pyqrcode Module – This Module helps to generate a QR Code.

2. Create the GUI Window:

window = Tk()  
window.geometry('300x350')
window.title('PythonGeeks')
 
Label(window,text='Let’s Create QR Code',font='arial 15').pack()
  • Tk() – This method helps to create a GUI window named window.
  • geometry() – This helps to specify the size of the window.
  • title() – This helps to specify the title of the window.
  • Label() – Label() is an inbuilt function of the Tkinter Module. It helps in the creation of a widget that will help us display a text. Here we can specify the size, font, background color, foreground color etc of this widget.
  • pack() – To display the Label(), we are going to use this method. While using pack(0 method, we don’t have to specify the x and y coordinates. The system automatically sets the Label on the window.

3. Create QR Code Generation Function:

def create_qrcode():
    s1=s.get()
    qr = pyqrcode.create(s1)
    qr.png('myqr.png', scale = 6)
    Label(window,text='QR Code is created and saved successfully').pack()
  • Using the get() function, we are getting the string value that the user is entering in the s1 variable.
  • pyqrcode.create() – This method helps us create a QR Code for the url in string s1. This is an inbuilt method of pyqrcode module.
  • png() – Using the png() method, we are going to save the qr code in the system. The extension of the qr code file will be png. After saving the image, we are going to display a text using the Label() method.

4. Create Button And Entry Field:

Entry(window,textvariable=s,font='arial 15').pack()
Button(window,text='create',bg='pink',command=create_qrcode).pack()
  • Entry() – This is an inbuilt function of the Tkinter Module. Entry() helps us create an entry field where the user will enter the url for which we are generating the QR Code. textvariable=s means that whatever text user inputs, will be saved in a variable s.
  • Button() – This is also an inbuilt function of the Tkinter Module. Button() is used to create a button on the window. We can specify the text on the button. Just like in a Label() , here too we can specify the color, font size etc of the button.
window.mainloop()
  • Mainloop() – This function makes the window displayed when the project is executed.

Hurray! We have created the QR Code Generator Project successfully. Now let us look at the output of the project.

Python QR Code Generator Output

This is how the window looks. Once you click on the create button, a QR Code is created and saved.

The QR Code is saved with the code.

python qr code generator output

Summary

We have successfully created the Python QR Code Generator Project. To create this project we used the Tkinter Module, Png Module, and Pyqrcode Module. Using these we created the project and now we can use the QR Code Generator to generate QR Code for any url.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google | Facebook


1 Response

  1. preeti says:

    it was so useful thanking so much

Leave a Reply

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