美文网首页码农的世界
Python图形界面实战:使用PyQt5/PiSide2制作二维

Python图形界面实战:使用PyQt5/PiSide2制作二维

作者: b4a0155c6514 | 来源:发表于2019-01-12 11:54 被阅读1次
    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    一、文中涉及

    本文涉及以下知识点:

    • PyQt5/PiSide2网格布局的使用;
    • PyQt5/PiSide2按钮小部件的使用;
    • PyQt5/PiSide2标签小部件的使用;
    • PyQt5/PiSide2选值框小部件的使用;
    • PyQt5/PiSide2图像的使用;
    • Python第三方库qrcode的使用;

    为了方便起见,以下代码使用PyQt5进行讲解,PiSide2同样适用。

    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    二、创建图形界面

    首先,我们来创建这个二维码生成器的基础图形界面。界面采用QGridLayout()网格布局,里面包含5个QLable()文本标签部件:

    • 指示输入二维码的内容;
    • 指示选择二维码图像尺寸;
    • 指示设置二维码的内边框;
    • 指示预览二维码;
    • 显示预览的二维码;

    首先我们引入所需的模块:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from PyQt5 import QtWidgets,QtGui
    import qrcode
    import io
    import traceback
    import datetime

    </pre>

    为了方便大家理解各个子类调用的位置,我们没有从PyQt5的子模块中直接import所有的子类。

    然后创建一个继承自QtWidgets.QWidget()的类——QrcodeApp()作为我们的图形界面的主类,将上述的小部件添加进去:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class QrcodeApp(QtWidgets.QWidget):
    def init(self):
    super().init()
    self.setFixedSize(450,220)
    self.setWindowTitle("州的先生 - 二维码生成器")
    self.main_layout = QtWidgets.QGridLayout() # 窗口网格布局
    self.label_desc = QtWidgets.QLabel("输入文字或网址:")
    self.input_content = QtWidgets.QLineEdit() # 二维码内容
    self.input_content.setText("州的先生")

    self.lable_size = QtWidgets.QLabel("图片尺寸:")
    self.label_space = QtWidgets.QLabel("留白设置:")
    self.lable_pre = QtWidgets.QLabel("预览:")
    self.input_size = QtWidgets.QComboBox() # 二维码编码类型
    self.input_size.addItem("210210")
    self.input_size.addItem("420
    420")
    self.input_size.addItem("630*630")

    self.input_space = QtWidgets.QSpinBox() # 二维码留白大小
    self.save_qrcode = QtWidgets.QPushButton("保存二维码")
    self.qrcode_img = QtWidgets.QLabel()
    self.qrcode_img.setScaledContents(True) # 设置二维码图像容器内部件缩放
    self.qrcode_img.setMaximumSize(190,190) # 固定二维码图像容器大小

    self.setLayout(self.main_layout)
    self.main_layout.addWidget(self.label_desc,0,0,1,2)
    self.main_layout.addWidget(self.input_content, 1, 0, 1, 2)
    self.main_layout.addWidget(self.lable_size, 2, 0, 1, 1)
    self.main_layout.addWidget(self.label_space, 2, 1, 1, 1)
    self.main_layout.addWidget(self.input_size, 3, 0, 1, 1)
    self.main_layout.addWidget(self.input_space, 3, 1, 1, 1)
    self.main_layout.addWidget(self.save_qrcode,4,0,1,2)
    self.main_layout.addWidget(self.lable_pre,0,2,)
    self.main_layout.addWidget(self.qrcode_img, 1, 2, 5, 2)

    </pre>

    这样,在运行代码后,我们会得到一个如下图所示的图形界面窗口:

    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    一个基本的界面创建好了,接下来我们来实现生成二维码的功能;

    三、生成二维码

    在Python中有很多第三方的库可以生成二维码,在此我们选用的的qrcode这个库。没有按照的小伙伴可以使用下述命令进行安装:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install qrcode

    它的项目地址为:https://github.com/lincolnloop/python-qrcode ,里面有详细的使用方法。
    

    </pre>

    qrcode安装好之后,我们就可以很方便地创建二维码了。以下是一个使用qrcode创建二维码简单的示例:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import qrcode

    img = qrcode.make('https://zmister.com')
    img.save('qrcode.png')

    </pre>

    运行上述代码,我们将会在本地目录得到一个名为qrcode.png的二维码图片,使用微信扫描它就可以直接打开州的先生博客。但是我们创建的二维码没有那么简单,还需要设置二维码的大小和留白,所以我们使用qrcode的高级用法。

    在QrcodeApp()类中新建一个名为value_change()的方法,分别获取文本输入框、选值框和下拉框的值作为二维码的内容、二维码的留白大小和二维码的图片大小。通过qrcode生成二维码,再通过Qlabel()部件将二维码显示出来:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def value_change(self):
    try:
    qr_text = self.input_content.text() # 二维码内容值
    qr_space = self.input_space.text()
    qr_size = int(self.input_size.currentText().split('*')[0])
    print(qr_text)
    qr = qrcode.QRCode(
    version=1,
    box_size=qr_size / 21,
    border=qr_space if qr_space != '' else 0
    )
    qr.add_data(qr_text)
    self.qr_img = qr.make_image()

    fp = io.BytesIO()
    self.qr_img.save(fp,"BMP")
    image = QtGui.QImage()
    image.loadFromData(fp.getvalue(),"BMP")
    qr_pixmap = QtGui.QPixmap.fromImage(image)
    self.qrcode_img.setPixmap(qr_pixmap)
    except Exception as e:
    print(repr(e))
    print(traceback.print_exc())

    </pre>

    然后在初始化方法中调用value_change()方法,运行程序,我们就可以看到默认的文本框内容“州的先生”就已经被生成了二维码,如下图所示:

    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    接下来,我们将文本输入框的文本改变信号textChanged、下拉框的当前索引改变信号currentIndexChanged和选值框的值改变信号valueChanged都连接到value_change()方法上,使得二维码可以实时根据设置的变化而生成新的二维码:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">self.input_content.textChanged.connect(self.value_change)
    self.input_size.currentIndexChanged.connect(self.value_change)
    self.input_space.valueChanged.connect(self.value_change)

    </pre>

    再次运行代码,可以发现当我们改变文本数据框和选值框的值时,显示的二维码也随之发生了改变,如下图所示:

    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    四、保存二维码

    完成了二维码的生成之后,我们需要将其保存到本地。在这里我们使用PyQt5的文件保存弹出框和qrcode的二维码保存方法save()来实现,继续新建一个名为save_qr()的方法:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def save_qr(self):
    date = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M%S%f')
    filename = QtWidgets.QFileDialog.getSaveFileName(self,'保存二维码','./qrcode-{date}.png'.format(date=date),'图像文件(*.png)')
    if filename[0] != '':
    self.qr_img.save(filename[0])
    print("保存成功")
    QtWidgets.QDialog().show()

    </pre>

    然后将“保存二维码”按钮的clicked信号连接到这个方法上:

    <pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">self.save_qrcode.clicked.connect(self.save_qr)

    </pre>

    最后运行代码,可以发现生成的二维码已经可以保存了,如下图所示:

    Python图形界面实战:使用PyQt5/PiSide2制作二维码生成器

    五、最后

    这样我们就实现了使用Python制作二维码生成

    相关文章

      网友评论

        本文标题:Python图形界面实战:使用PyQt5/PiSide2制作二维

        本文链接:https://www.haomeiwen.com/subject/nbnqdqtx.html