The way to get the personalized QR code exe desktop application I put at the end of the article, pay attention to check it. By executing the packaged exe application you can run it directly to generate the personalized QR code.
Before we start, let's see how personalized QR codes are generated through a QR code generator.
The python packages used in this are the same modules used in the previous GUI application creation.
# -*- coding:utf-8 -*- import os import sys from import * from import * from import * import images
Here the images module is used to solve the problem that references to external images don't work when packaging an application. A later article will explain how to package external resources into an exe application.
Doing GUI desktop application, the first thing is still using pyqt5 for the layout of the interface and the addition of interface components, although the amount of code seems to be more, but the logic is not much.
# -*- 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('') self.words_text.setAttribute(Qt.WA_InputMethodEnabled, False) self.version_label = QLabel() self.version_label.setText('Margin settings (only fine tuning allowed):') self.version_text = QSpinBox() self.version_text.setRange(1, 3) self.version_text.setValue(1) self.picture_text = QLineEdit() self.picture_text.setPlaceholderText('Personalized image path') self.picture_text.setReadOnly(True) self.picture_button = QPushButton() self.picture_button.setText('Personalized pictures') self.picture_button.(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.(self.save_dir_button_click) self.generate_button = QPushButton() self.generate_button.setText('Quickly generate QR codes') self.generate_button.(self.generate_button_click) self.version_current = QLabel() self.version_current.setText('The default QR code is the author's public number, version statement: this app is released by the public number [Python Concentration Camp]!') self.version_current.setAlignment() self.version_current.setStyleSheet('color:red') = QLabel() (True) (200, 200) (QPixmap(':/')) (self.words_label, 0, 0, 1, 1) (self.words_text, 0, 1, 1, 2) (self.version_label, 1, 0, 1, 2) (self.version_text, 1, 2, 1, 1) (self.picture_text, 2, 0, 1, 2) (self.picture_button, 2, 2, 1, 1) (self.colorized_label, 3, 0, 1, 2) (self.colorized_text, 3, 2, 1, 1) (self.brightness_label, 4, 0, 1, 2) (self.brightness_text, 4, 2, 1, 1) (self.save_dir_text, 5, 0, 1, 2) (self.save_dir_button, 5, 2, 1, 1) (self.generate_button, 6, 0, 1, 3) hbox = QHBoxLayout() () (30) (grid) vbox = QVBoxLayout() (hbox) (10) (self.version_current) (vbox)
Used to the slot function there are three, one is to do to select the background image, the second is to do to choose to store the generated file storage path can be freely selected to where to store, the third is to do to generate the QR code up function.
The first one looks at how to achieve reading a background image that needs to be used as a personalized QR code by using an associative slot function.
def picture_button_click(self): import os = () txt_file_path = (self, "Selected documents", , "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 the slot function that selects the path to the stored file.
def save_dir_button_click(self): import os = () directory = (self, 'Select folder', ) print(directory) self.save_dir_text.setText(directory)
It is to get the path to the custom selected storage file in the form of a dialog box.
The third slot function is to generate a personalized QR code, in fact, the QR code generation part is only one line of code. That is the run function provided by the MYQR module, through which the personalized QR code can be generated.
First, you need to import the library MYQR.
from MyQR import myqr
In order to be able to see the back of the QR code generation function (run function), first look at the library provides run function have what parameters.
''' () Parameter explanation words The link or text to be jumped. version natural number, the larger the number, the larger the side length. level Error correction level picture Combine with picture colorized Whether to display color or not. contrast contrast ratio, default is 1.0 brightness float, adjust the brightness of the picture save_name output file name, default is "". save_dir storage location, default is current directory. save_dir
Here is a look at this specific generation of personalized QR code slot function. In addition to the generation of the QR code and the need to display the generated QR code on the application's page, the other main parameters of the checking method.
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 = ()
print(save_dir)
(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 + '/' + ''
print(map_dir)
(QPixmap(map_dir))
To this article on the production of a two-dimensional code generator based on PyQT5 article on this, more related PyQT5 two-dimensional code generator content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!