pyqt5 has made a QR code generator, which has been packaged into an exe executable program

The way to obtain the EXE desktop application of personalized QR code is put at the end of the article. Please check it. The packaged exe application can be run directly to generate personalized QR code.

Before we start, let's take a look at how to generate personalized QR codes through QR code generator.

[read the full text]

The python package used is the same as the module used in the previous GUI application production.

# -*- coding:utf-8 -*-
import os

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

import images

The images module here is used to solve the problem that the reference of external pictures cannot take effect when packaging applications. A later article will explain how to package external resources into the application of exe.

As a desktop application of GUI, you should first use pyqt5 to layout the interface and add interface components. Although the amount of code seems to be large, there is not much logic.

# -*- coding:utf-8 -*-
    def init_ui(self):
        grid = QGridLayout()

        self.picture_name = ''
        self.words_label = QLabel()
        self.words_label.setText('Link settings:')
        self.words_text = QLineEdit()
        self.words_text.setPlaceholderText('www.baidu.com')
        self.words_text.setAttribute(Qt.WA_InputMethodEnabled, False)

        self.version_label = QLabel()
        self.version_label.setText('Margin setting (fine tuning only):')
        self.version_text = QSpinBox()
        self.version_text.setRange(1, 3)
        self.version_text.setValue(1)

        self.picture_text = QLineEdit()
        self.picture_text.setPlaceholderText('Personalized picture path')
        self.picture_text.setReadOnly(True)
        self.picture_button = QPushButton()
        self.picture_button.setText('Personalized picture')
        self.picture_button.clicked.connect(self.picture_button_click)

        self.colorized_label = QLabel()
        self.colorized_label.setText('Whether to display in color:')
        self.colorized_text = QComboBox()
        colorized_items = ['yes', 'no']
        self.colorized_text.addItems(colorized_items)
        self.colorized_text.setCurrentIndex(1)

        self.brightness_label = QLabel()
        self.brightness_label.setText('Adjust picture brightness:')
        self.brightness_text = QDoubleSpinBox()
        self.brightness_text.setRange(1, 10)
        self.brightness_text.setSingleStep(1.0)

        self.save_dir_text = QLineEdit()
        self.save_dir_text.setPlaceholderText('Storage directory')
        self.save_dir_text.setReadOnly(True)
        self.save_dir_button = QPushButton()
        self.save_dir_button.setText('Custom path')
        self.save_dir_button.clicked.connect(self.save_dir_button_click)

        self.generate_button = QPushButton()
        self.generate_button.setText('Quickly generate QR code')
        self.generate_button.clicked.connect(self.generate_button_click)

        self.version_current = QLabel()
        self.version_current.setText('The default two-dimensional code is the author's official account. The version is declared: the official account is the application. [Python concentration camp] release!')
        self.version_current.setAlignment(Qt.AlignCenter)
        self.version_current.setStyleSheet('color:red')

        self.image = QLabel()
        self.image.setScaledContents(True)
        self.image.setMaximumSize(200, 200)
        self.image.setPixmap(QPixmap(':/default.png'))

        grid.addWidget(self.words_label, 0, 0, 1, 1)
        grid.addWidget(self.words_text, 0, 1, 1, 2)
        grid.addWidget(self.version_label, 1, 0, 1, 2)
        grid.addWidget(self.version_text, 1, 2, 1, 1)
        grid.addWidget(self.picture_text, 2, 0, 1, 2)
        grid.addWidget(self.picture_button, 2, 2, 1, 1)
        grid.addWidget(self.colorized_label, 3, 0, 1, 2)
        grid.addWidget(self.colorized_text, 3, 2, 1, 1)
        grid.addWidget(self.brightness_label, 4, 0, 1, 2)
        grid.addWidget(self.brightness_text, 4, 2, 1, 1)
        grid.addWidget(self.save_dir_text, 5, 0, 1, 2)
        grid.addWidget(self.save_dir_button, 5, 2, 1, 1)
        grid.addWidget(self.generate_button, 6, 0, 1, 3)

        hbox = QHBoxLayout()
        hbox.addWidget(self.image)
        hbox.addSpacing(30)
        hbox.addLayout(grid)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        vbox.addSpacing(10)
        vbox.addWidget(self.version_current)

        self.setLayout(vbox)

There are three slot functions used. One is to select the background picture, the second is to select the storage path to store the generated file. You can freely choose where to store it, and the third is to call the function to generate QR code.

First, let's take a look at how to read the background image that needs to be used as a personalized QR code through the associated slot function.

    def picture_button_click(self):
        import os
        self.cwd = os.getcwd()
        txt_file_path = QFileDialog.getOpenFileName(self, "Select file", self.cwd, "JPG File (*.jpg);; PNG File (*.png)")
        self.picture_text.setText(txt_file_path[0])
        if self.picture_text.text().strip() != "":
            self.picture_name = txt_file_path[0].split('/')[-1].split('.')[0]
            print(self.picture_name)
        else:
            self.picture_name = ''

The second is to select the slot function to store the file path.

    def save_dir_button_click(self):
        import os
        self.cwd = os.getcwd()
        directory = QFileDialog.getExistingDirectory(self, 'Select Folder', self.cwd)
        print(directory)
        self.save_dir_text.setText(directory)

The user-defined storage file path is obtained through the dialog box.

The third slot function is to generate personalized QR code. In fact, the generation part of QR code only has one sentence of code. That is the run function provided by MYQR module, which can realize the generation of personalized QR code.

First, you need to import the MYQR library.

from MyQR import myqr

In order to see the QR code generation function (run function) clearly, let's take a look at the parameters of the run function provided by this library.

'''
    myqr.run()  Parameter interpretation
    words        Link or text to jump
    version        Natural number, the larger the number, the larger the side length
    level        Error correction level
    picture        Combined picture
    colorized    Show color
    contrast    Contrast, the default is 1.0
    brightness    brightness  float,Adjust the brightness of the picture
    save_name    Output file name, default file name"qrcode.png"
    save_dir    Storage location. The current directory is stored by default
'''

Let's take a look at the slot function for generating personalized QR codes. In addition to the generation part of QR code and the need to display the generated QR code on the application page, other main methods are some parameter verification methods.

  def generate_button_click(self):
        from MyQR import myqr
        colorized_index = self.colorized_text.currentIndex()
        print(colorized_index)
        colorized = None
        if colorized_index == 0:
            colorized = True
        else:
            colorized = False
        print(colorized)
        words_text = self.words_text.text()
        words = None
        if words_text.strip() != "":
            words = words_text.strip()
        else:
            words = 'default message: Python is very beautiful'
        print(words)
        version_text = self.version_text.value()
        print(version_text)
        picture_text = self.picture_text.text()
        picture = None
        if picture_text.strip() != "":
            picture = picture_text
        print(picture)
        brightness_text = self.brightness_text.value()
        print(brightness_text)
        save_dir_text = self.save_dir_text.text()
        save_dir = None
        if save_dir_text.strip() != "":
            save_dir = save_dir_text.strip()
        else:
            save_dir = os.getcwd()
        print(save_dir)
        myqr.run(words=str(words), version=int(version_text), level='H', picture=picture,
                 colorized=colorized, contrast=1.0, brightness=float(brightness_text), save_dir=save_dir)
        if self.picture_name.strip() != '':
            map_dir = save_dir + '/' + self.picture_name + '_qrcode.png'
        else:
            map_dir = save_dir + '/' + 'qrcode.png'
        print(map_dir)
        self.image.setPixmap(QPixmap(map_dir))

There seems to be a lot of code, and there is no hard to understand logical processing. If there are problems or better solutions, you can leave a message in the comment area for discussion~

exe desktop application acquisition method of personalized 2D code generator: official account returns "2D code generator" to receive. Due to the length of the code, the next article will introduce how to package external resources, pictures, etc. into the application of PyQt5.

[selected from previous periods]

How to implement a data management system in the console (including the addition, deletion, modification and query of MYSQL database)

Self made document format converter, support txt/.xlsx/.csv format conversion

How does PyPDF2 extract and save as a PDF file according to the PDF page number?

Romantic turtle, give programmers their own Christmas tree!

PyQt5 GUI: Baidu picture downloader (source code attached at the end of the text)

Keywords: Python

Added by the-hardy-kid on Sun, 02 Jan 2022 11:32:56 +0200