Python 3 PyQt5 Script to Build QR Code Generator GUI Desktop App Full Project For Beginners



Welcome folks today in this blog post we will be building a qr code generator in python using pyqt5 library. All the full source code of the application is shown below.




Get Started




In order to get started you need to install the below library using the pip command as shown below



pip install qrcode



pip install pyqt5



After installing these libraries you need to make an file and copy paste the following code



# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qrcode
import sys

# Image class for QR code
class Image(qrcode.image.base.BaseImage):

    # constructor
    def __init__(self, border, width, box_size):

        # assigning border
        self.border = border

        # assigning width
        self.width = width

        # assigning box size
        self.box_size = box_size

        # creating size
        size = (width + border * 2) * box_size

        # image
        self._image = QImage(size, size, QImage.Format_RGB16)

        # initial image as white

    # pixmap method
    def pixmap(self):

        # returns image
        return QPixmap.fromImage(self._image)

    # drawrect method for drawing rectangle
    def drawrect(self, row, col):

        # creating painter object
        painter = QPainter(self._image)

        # drawing rectangle
            (col + self.border) * self.box_size,
            (row + self.border) * self.box_size,
            self.box_size, self.box_size,

# Main Window class
class Window(QMainWindow):

    # constructor
    def __init__(self):

        # setting window title
        self.setWindowTitle("QR Code")

        # setting geometry
        self.setGeometry(100, 100, 300, 300)

        # creating a label to show the qr code
        self.label = QLabel(self)

        # creating a line edit to receive text
        self.edit = QLineEdit(self)

        # adding action when entered is pressed

        # setting font to the line edit
        self.edit.setFont(QFont('Times', 9))

        # setting alignment

        # creating a vertical layout
        layout = QVBoxLayout(self)

        # adding label to the layput

        # adding line edit to the layout

        # creating a QWidget object
        widget = QWidget()

        # setting layout to the widget

        # setting widget as central widget to the main window

    # method called by the line edit
    def handleTextEntered(self):

        # get the text
        text = self.edit.text()

        # creating a pix map of qr code
        qr_image = qrcode.make(text, image_factory = Image).pixmap()

        # set image to the label

# create pyqt5 app
app = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# showing window

# start the app

Leave a Reply