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.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.picture_text = QLineEdit()
        self.picture_text.setPlaceholderText('Personalized picture path')
        self.picture_button = QPushButton()
        self.picture_button.setText('Personalized picture')

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

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

        self.save_dir_text = QLineEdit()
        self.save_dir_text.setPlaceholderText('Storage directory')
        self.save_dir_button = QPushButton()
        self.save_dir_button.setText('Custom path')

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

        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.image = QLabel()
        self.image.setMaximumSize(200, 200)

        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()

        vbox = QVBoxLayout()


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)")
        if self.picture_text.text().strip() != "":
            self.picture_name = txt_file_path[0].split('/')[-1].split('.')[0]
            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)

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.

'''  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()
        colorized = None
        if colorized_index == 0:
            colorized = True
            colorized = False
        words_text = self.words_text.text()
        words = None
        if words_text.strip() != "":
            words = words_text.strip()
            words = 'default message: Python is very beautiful'
        version_text = self.version_text.value()
        picture_text = self.picture_text.text()
        picture = None
        if picture_text.strip() != "":
            picture = picture_text
        brightness_text = self.brightness_text.value()
        save_dir_text = self.save_dir_text.text()
        save_dir = None
        if save_dir_text.strip() != "":
            save_dir = save_dir_text.strip()
            save_dir = os.getcwd()
        print(save_dir), 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'
            map_dir = save_dir + '/' + 'qrcode.png'

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