📌  相关文章
📜  使用 PyQt5 构建二维码生成器应用程序

📅  最后修改于: 2022-05-13 01:55:07.464000             🧑  作者: Mango

使用 PyQt5 构建二维码生成器应用程序

在本文中,我们将了解如何使用 PyQt5 制作 QR 码生成器应用程序。二维码是一种矩阵条码,于 1994 年首次为日本的汽车行业设计。条形码是一种机器可读的光学标签,其中包含有关其所附物品的信息。下面是应用程序的外观

为了做到这一点,我们将使用下面给出的库
PyQt5是跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。下面是安装 PyQt5 的命令

pip install PyQt5

qrcode :用于生成快速响应代码是一种二维象形代码,用于其快速可读性和较大的存储容量。该代码由在白色背景上以方形图案排列的黑色模块组成。编码的信息可以由任何类型的数据(例如,二进制、字母数字或汉字符号)组成。下面是安装二维码模块的命令

pip install qrcode

下面是实现

Python3
# 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
        self._image.fill(Qt.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
        painter.fillRect(
            (col + self.border) * self.box_size,
            (row + self.border) * self.box_size,
            self.box_size, self.box_size,
            QtCore.Qt.black)
 
 
# Main Window class
class Window(QMainWindow):
 
    # constructor
    def __init__(self):
        QMainWindow.__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
        self.edit.returnPressed.connect(self.handleTextEntered)
 
        # setting font to the line edit
        self.edit.setFont(QFont('Times', 9))
 
        # setting alignment
        self.edit.setAlignment(Qt.AlignCenter)
 
        # creating a vertical layout
        layout = QVBoxLayout(self)
 
        # adding label to the layout
        layout.addWidget(self.label)
 
        # adding line edit to the layout
        layout.addWidget(self.edit)
 
        # creating a QWidget object
        widget = QWidget()
 
        # setting layout to the widget
        widget.setLayout(layout)
 
        # setting widget as central widget to the main window
        self.setCentralWidget(widget)
 
 
    # 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
        self.label.setPixmap(qr_image)
 
 
 
 
# create pyqt5 app
app = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# showing window
window.show()
 
# start the app
sys.exit(app.exec_())


输出 :

当用户在行编辑中输入文本并按回车键时,将显示二维码,窗口大小将根据二维码的大小进行调整