基于PyQT5制作一个二维码生成器

这篇文章主要介绍了利用PyQT5制作一个简单的二维码生成器,并打包成exe可执行程序。文中的示例代码讲解详细,感兴趣的同学可以了解一下。编程学习资料点击领取

个性化二维码的exe桌面应用的获取方式我放在文章最后面了,注意查收。通过执行打包后的exe应用程序可以直接运行生成个性化二维码。

开始之前先来看一下通过二维码生成器是如何生成个性化二维码的。

基于PyQT5制作一个二维码生成器_第1张图片

基于PyQT5制作一个二维码生成器_第2张图片

其中使用的python包和之前的GUI应用制作使用的模块是一样的。

1

2

3

4

5

6

7

8

9

10

11

12

# -*- coding:utf-8 -*-

import os

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

import images

这里的images模块是用于解决打包应用时外部图片的引用不能生效的问题。后面的一篇文章中将会说明如何将外部资源打包到exe的应用中去。

做GUI的桌面应用,首先还是使用pyqt5进行界面的布局和界面组件的添加,虽然代码量看起来比较多,但逻辑不多。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

# -*- coding:utf-8 -*-

    def init_ui(self):

        grid = QGridLayout()

        self.picture_name = ''

        self.words_label = QLabel()

        self.words_label.setText('链接设置:')

        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('边距设置(只允许微调):')

        self.version_text = QSpinBox()

        self.version_text.setRange(1, 3)

        self.version_text.setValue(1)

        self.picture_text = QLineEdit()

        self.picture_text.setPlaceholderText('个性化图片路径')

        self.picture_text.setReadOnly(True)

        self.picture_button = QPushButton()

        self.picture_button.setText('个性化图片')

        self.picture_button.clicked.connect(self.picture_button_click)

        self.colorized_label = QLabel()

        self.colorized_label.setText('是否显示为彩色:')

        self.colorized_text = QComboBox()

        colorized_items = ['是', '否']

        self.colorized_text.addItems(colorized_items)

        self.colorized_text.setCurrentIndex(1)

        self.brightness_label = QLabel()

        self.brightness_label.setText('调节图片亮度:')

        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('存储目录')

        self.save_dir_text.setReadOnly(True)

        self.save_dir_button = QPushButton()

        self.save_dir_button.setText('自定义路径')

        self.save_dir_button.clicked.connect(self.save_dir_button_click)

        self.generate_button = QPushButton()

        self.generate_button.setText('快速生成二维码')

        self.generate_button.clicked.connect(self.generate_button_click)

        self.version_current = QLabel()

        self.version_current.setText

        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)

用到的槽函数有三个,一个是为了做选择背景图片、第二个是为了做选择要存储生成后的文件存放路径可以自由选择存放到什么地方、第三个是为了做调起生成二维码的函数。

第一个来看一下如何通过关联槽函数来实现读取需要作为个性化二维码的背景图片。

1

2

3

4

5

6

7

8

9

10

def picture_button_click(self):

    import os

    self.cwd = os.getcwd()

    txt_file_path = QFileDialog.getOpenFileName(self, "选取文件", 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 = ''

第二个就是选择存储文件路径的槽函数。

1

2

3

4

5

6

def save_dir_button_click(self):

    import os

    self.cwd = os.getcwd()

    directory = QFileDialog.getExistingDirectory(self, '选取文件夹', self.cwd)

    print(directory)

    self.save_dir_text.setText(directory)

是通过dialog对话框的形式获取到自定义选择的存储文件路径。

第三个槽函数就是为了生成个性化二维码,其实二维码的生成部分只有一句代码。那就是MYQR模块提供的run函数,通过这个函数就能实现个性化二维码的生成。

首先,需要导入MYQR这个库。

1

from MyQR import myqr

为了可以看清楚后面二维码生成函数(run函数),先来看一下这个库提供的run函数都有什么参数。

1

2

3

4

5

6

7

8

9

10

11

12

'''

    myqr.run()  参数解释

    words        需要跳转的链接或者文字

    version        自然数,数字越大边长越大

    level        纠错等级

    picture        结合图片

    colorized    是否显示彩色

    contrast    对比度,默认为1.0

    brightness    亮度  float,调节图片的亮度

    save_name    输出文件名,默认文件名"qrcode.png"

    save_dir    存储位置,默认存储当前目录

'''

下面看一下这个具体生成个性化二维码的槽函数。除了二维码的生成部分和需要将生成后的二维码放到应用的页面上展示之外,其他主要就是一些参数的校验方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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))="Arial, Verdana, sans-serif">="white-space: normal;"> </span></font>

到此这篇关于基于PyQT5制作一个二维码生成器的文章就介绍到这了。

基于PyQT5制作一个二维码生成器_第3张图片

基于PyQT5制作一个二维码生成器_第4张图片 

 

基于PyQT5制作一个二维码生成器_第5张图片 

基于PyQT5制作一个二维码生成器_第6张图片 

基于PyQT5制作一个二维码生成器_第7张图片 

 

你可能感兴趣的:(Python实战案列,qt,开发语言,python,程序人生,职场和发展)