pyside6逻辑
1、designer设计好UI,选择合理的组件;
2、编译好文件,准备主函数;
3、主函数引用UI文件,将函数(信号)绑定到(槽)组件;
实践
1、使用designer设计好UI,然后编译成Ui_myQR.py文件
# -*- 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)
#if QT_CONFIG(statustip)
self.lineEdit.setStatusTip(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u5185\u5bb9", None))
#endif // QT_CONFIG(statustip)
self.lineEdit.setPlaceholderText(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u82f1\u6587\u6216\u94fe\u63a5", None))
self.radioButton.setText(QCoreApplication.translate("Form", u"\u5f69\u8272/\u9ed1\u767d", None))
self.label_2.setText(QCoreApplication.translate("Form", u"\u7ea0\u9519\u6c34\u5e73", None))
self.jcBox.setCurrentText("")
self.scbox.setText(QCoreApplication.translate("Form", u"\u4e0a\u4f20\u5e95\u56fe", None))
self.scbox_2.setText(QCoreApplication.translate("Form", u"\u63d0\u4ea4\u751f\u6210", None))
# retranslateUi
2、编写主程序Qrun.py文件
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)
self.radioButton.toggled.connect(self.on_radio_button_toggled)
def on_radio_button_toggled(self):
self.radioButton.setChecked(self.radioButton.isChecked())
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=10, # 设置容错率
level=self.jcBox.currentText(), # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
picture=self.scbox.text(), # 背景图片所在目录 图片所在目录,可以是动图
colorized=self.radioButton.isChecked(), # 黑白(False)还是彩色(True)
contrast=1.0, # 用以调节图片的对比度,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()
# pyinstaller --onefile --windowed QRrun.py --icon=tlp.ico
3、打包发布
pyinstaller --onefile --name=MyQR --icon=icon.ico --windowed Qrun.py
评论区