pyside6小案例:二维生成器

查看 23|回复 2
作者:vicmay   
首先设计界面
然后生成编译好UI_myQR.py文件,如下
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'myQR.ui'
##
## Created by: Qt User Interface Compiler version 6.7.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QComboBox, QHBoxLayout, QLabel,
    QLineEdit, QPushButton, QRadioButton, QSizePolicy,
    QVBoxLayout, QWidget)
class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(430, 302)
        self.lineEdit = QLineEdit(Form)
        self.lineEdit.setObjectName(u"lineEdit")
        self.lineEdit.setGeometry(QRect(10, 20, 411, 101))
        self.layoutWidget = QWidget(Form)
        self.layoutWidget.setObjectName(u"layoutWidget")
        self.layoutWidget.setGeometry(QRect(10, 130, 411, 161))
        self.verticalLayout = QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.radioButton = QRadioButton(self.layoutWidget)
        self.radioButton.setObjectName(u"radioButton")
        self.radioButton.setLayoutDirection(Qt.LayoutDirection.LeftToRight)
        self.radioButton.setAutoFillBackground(False)
        self.radioButton.setChecked(True)
        self.radioButton.setAutoRepeat(False)
        self.radioButton.setAutoExclusive(False)
        self.radioButton.setAutoRepeatDelay(200)
        self.horizontalLayout.addWidget(self.radioButton)
        self.label_2 = QLabel(self.layoutWidget)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setLayoutDirection(Qt.LayoutDirection.RightToLeft)
        self.label_2.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.horizontalLayout.addWidget(self.label_2)
        self.jcBox = QComboBox(self.layoutWidget)
        self.jcBox.setObjectName(u"jcBox")
        self.jcBox.setEditable(False)
        self.horizontalLayout.addWidget(self.jcBox)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_3 = QHBoxLayout()
        self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
        self.scbox = QPushButton(self.layoutWidget)
        self.scbox.setObjectName(u"scbox")
        self.horizontalLayout_3.addWidget(self.scbox)
        self.scbox_2 = QPushButton(self.layoutWidget)
        self.scbox_2.setObjectName(u"scbox_2")
        self.horizontalLayout_3.addWidget(self.scbox_2)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.retranslateUi(Form)
        QMetaObject.connectSlotsByName(Form)
    # setupUi
    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
#if QT_CONFIG(tooltip)
        Form.setToolTip("")
#endif // QT_CONFIG(tooltip)
#if QT_CONFIG(statustip)
        Form.setStatusTip("")
#endif // QT_CONFIG(statustip)
#if QT_CONFIG(whatsthis)
        Form.setWhatsThis("")
#endif // QT_CONFIG(whatsthis)
#if QT_CONFIG(tooltip)
        self.lineEdit.setToolTip(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u5185\u5bb9", None))
#endif // QT_CONFIG(tooltip)
# retranslateUi
最后开始编辑程序,QRun.py,如下:
[Python] 纯文本查看 复制代码
from PySide6.QtWidgets import *
from PySide6.QtGui import QPixmap
from Ui_myQR import Ui_Form
from MyQR import myqr
import os
import datetime
class Mywindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lineEdit.textChanged.connect(self.onTextChanged)
        self.jcBox.addItems(['L','M','Q','H'])
        self.jcBox.setCurrentIndex(3)
        self.scbox.clicked.connect(self.showDialog)
        self.scbox_2.clicked.connect(self.myqr_run)
    def onTextChanged(self):
        # 当文本编辑器中的文本发生变化时,更新关键字变量
        self.words = self.lineEdit.text()
    def showDialog(self):
        # 显示文件选择对话框
        fname = QFileDialog.getOpenFileName(self, 'Open file', 'C:/Users/15457/Desktop/测试文件')
        if fname[0]:
            print(f'File selected: {fname[0]}')
            self.scbox.setText(fname[0])  # 将选中的文件路径设置为 scbox 的文本
    def myqr_run(self):
        # 使用 os.path.join 组合路径和文件名
        save_dir, _ = os.path.split(self.scbox.text())
        timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        save_name = os.path.join(save_dir, f'{timestamp}.png')
        myqr.run(
            words=self.words,  # 扫描二维码后,显示的内容,或是跳转的链接
            version=5,  # 设置容错率
            level=self.jcBox.currentText(),  # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
            picture=self.scbox.text(),  # 背景图片所在目录 图片所在目录,可以是动图
            colorized=True,  # 黑白(False)还是彩色(True)
            contrast=1.5,  # 用以调节图片的对比度,1.0 表示原始图片。默认为1.0。
            brightness=1.2,  # 用来调节图片的亮度,用法同上。
            save_name=save_name  # 控制输出文件名,格式可以是 .jpg, .png ,.bmp ,.gif
        )
        self.show_qr_code(save_name)
    def show_qr_code(self, image_path):
        msg_box = QMessageBox()
        pixmap = QPixmap(image_path)
        msg_box.setWindowTitle('二维码生成成功')
        msg_box.setIconPixmap(pixmap)
        msg_box.exec()
if __name__ == '__main__':
    app = QApplication([])
    window = Mywindow()
    window.show()
    app.exec()
基本逻辑是:先将组件初始化,然后利用组件属性获取值(信号),绑定到主要函数(槽)。了解这个逻辑,基本可以自己结合AI助手完成小项目了,最后可以打包成执行文件效果如图:

文本, 文件

vicmay
OP
  

代码插入推荐使用置顶帖的方式
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
vicmay
OP
  


苏紫方璇 发表于 2024-8-22 11:53
代码插入推荐使用置顶帖的方式
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn ...

谢谢指引
您需要登录后才可以回帖 登录 | 立即注册

返回顶部