业余时间学Python,打发时间。这次算是做了个练习,主要用到pyqt5、qrcode、PIL库。学Python太难了,感觉越学越迷糊,教程书都是入门的东西,真正做起小程序来根本就搞不来,一顿百度操作。说实在,很多东西都没有具体的讲解,纯粹靠蒙。
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QFont, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox , QFileDialog , QColorDialog
from PyQt5.QtGui import QPixmap
import sys, os, qrcode, ihelp, idata
from PIL import Image , ImageQt , ImageEnhance , ImageDraw
class MyLabel(QtWidgets.QLabel): #重载标签类,添加双击事件信号
DoubleClicked = QtCore.pyqtSignal() #自定义信号
def __init__(self, parent=None):
super(MyLabel, self).__init__(parent)
def mouseDoubleClickEvent(self, event): #双击事件
if event.buttons () == QtCore.Qt.LeftButton:
self.DoubleClicked.emit() #发射信号
class Ui_MainWindow(QWidget):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(750, 545)
MainWindow.setFixedSize(MainWindow.width(), MainWindow.height()) #固定窗口大小,取消最大化
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("source/icon.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.setWindowIcon(QtGui.QIcon("source/icon.ico"))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_creatqrcode = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_creatqrcode.setGeometry(QtCore.QRect(570, 370, 81, 31))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("source/creat.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_creatqrcode.setIcon(icon1)
self.pushButton_creatqrcode.setObjectName("pushButton_creatqrcode")
self.label_qrcode_img = QtWidgets.QLabel(self.centralwidget)
self.label_qrcode_img.setGeometry(QtCore.QRect(400, 10, 341, 341))
self.label_qrcode_img.setFrameShape(QtWidgets.QFrame.Box)
self.label_qrcode_img.setFrameShadow(QtWidgets.QFrame.Raised)
self.label_qrcode_img.setAlignment(QtCore.Qt.AlignCenter)
self.label_qrcode_img.setObjectName("label_qrcode_img")
self.pushButton_saveqrcode = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_saveqrcode.setGeometry(QtCore.QRect(660, 370, 81, 31))
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("source/save.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_saveqrcode.setIcon(icon2)
self.pushButton_saveqrcode.setObjectName("pushButton_saveqrcode")
self.pushButton_about = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_about.setGeometry(QtCore.QRect(660, 470, 81, 31))
self.pushButton_about.setObjectName("pushButton_about")
self.pushButton_help = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_help.setGeometry(QtCore.QRect(570, 470, 81, 31))
self.pushButton_help.setObjectName("pushButton_help")
self.groupBox_center_pic = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_center_pic.setGeometry(QtCore.QRect(10, 440, 251, 101))
self.groupBox_center_pic.setChecked(False)
self.groupBox_center_pic.setObjectName("groupBox_center_pic")
self.label_center_picture = QtWidgets.QLabel(self.groupBox_center_pic)
self.label_center_picture.setGeometry(QtCore.QRect(10, 20, 71, 71))
self.label_center_picture.setFrameShape(QtWidgets.QFrame.Box)
self.label_center_picture.setAlignment(QtCore.Qt.AlignCenter)
self.label_center_picture.setObjectName("label_center_picture")
self.labe_2 = QtWidgets.QLabel(self.groupBox_center_pic)
self.labe_2.setGeometry(QtCore.QRect(160, 30, 24, 12))
self.labe_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_2.setObjectName("labe_2")
self.comboBox_pic_size = QtWidgets.QComboBox(self.groupBox_center_pic)
self.comboBox_pic_size.setGeometry(QtCore.QRect(190, 20, 51, 30))
self.comboBox_pic_size.setObjectName("comboBox_pic_size")
self.labe_3 = QtWidgets.QLabel(self.groupBox_center_pic)
self.labe_3.setGeometry(QtCore.QRect(160, 70, 24, 12))
self.labe_3.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_3.setObjectName("labe_3")
self.comboBox_pic_circle = QtWidgets.QComboBox(self.groupBox_center_pic)
self.comboBox_pic_circle.setGeometry(QtCore.QRect(190, 60, 51, 30))
self.comboBox_pic_circle.setObjectName("comboBox_pic_circle")
self.pushButton_addcenterpic = QtWidgets.QPushButton(self.groupBox_center_pic)
self.pushButton_addcenterpic.setGeometry(QtCore.QRect(90, 20, 51, 31))
self.pushButton_addcenterpic.setObjectName("pushButton_addcenterpic")
self.pushButton_delcenterpic = QtWidgets.QPushButton(self.groupBox_center_pic)
self.pushButton_delcenterpic.setGeometry(QtCore.QRect(90, 60, 51, 31))
self.pushButton_delcenterpic.setObjectName("pushButton_delcenterpic")
self.groupBox_bk_pic = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_bk_pic.setGeometry(QtCore.QRect(270, 360, 291, 181))
self.groupBox_bk_pic.setObjectName("groupBox_bk_pic")
self.comboBox_bk_contrast = QtWidgets.QComboBox(self.groupBox_bk_pic)
self.comboBox_bk_contrast.setGeometry(QtCore.QRect(230, 140, 51, 30))
self.comboBox_bk_contrast.setObjectName("comboBox_bk_contrast")
self.labe_5 = QtWidgets.QLabel(self.groupBox_bk_pic)
self.labe_5.setGeometry(QtCore.QRect(180, 150, 36, 12))
self.labe_5.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_5.setObjectName("labe_5")
self.comboBox_bk_tran = QtWidgets.QComboBox(self.groupBox_bk_pic)
self.comboBox_bk_tran.setGeometry(QtCore.QRect(230, 100, 51, 30))
self.comboBox_bk_tran.setObjectName("comboBox_bk_tran")
self.labe_6 = QtWidgets.QLabel(self.groupBox_bk_pic)
self.labe_6.setGeometry(QtCore.QRect(180, 110, 36, 12))
self.labe_6.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_6.setObjectName("labe_6")
self.label_bk_picture = QtWidgets.QLabel(self.groupBox_bk_pic)
self.label_bk_picture.setGeometry(QtCore.QRect(10, 20, 151, 151))
self.label_bk_picture.setFrameShape(QtWidgets.QFrame.Box)
self.label_bk_picture.setAlignment(QtCore.Qt.AlignCenter)
self.label_bk_picture.setObjectName("label_bk_picture")
self.pushButton_addbkpic = QtWidgets.QPushButton(self.groupBox_bk_pic)
self.pushButton_addbkpic.setGeometry(QtCore.QRect(170, 20, 51, 31))
self.pushButton_addbkpic.setObjectName("pushButton_addbkpic")
self.pushButton_delbkpic = QtWidgets.QPushButton(self.groupBox_bk_pic)
self.pushButton_delbkpic.setGeometry(QtCore.QRect(230, 20, 51, 31))
self.pushButton_delbkpic.setObjectName("pushButton_delbkpic")
self.comboBox_bk_mode = QtWidgets.QComboBox(self.groupBox_bk_pic)
self.comboBox_bk_mode.setGeometry(QtCore.QRect(230, 60, 51, 30))
self.comboBox_bk_mode.setObjectName("comboBox_bk_mode")
self.labe_10 = QtWidgets.QLabel(self.groupBox_bk_pic)
self.labe_10.setGeometry(QtCore.QRect(190, 70, 24, 12))
self.labe_10.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_10.setObjectName("labe_10")
self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_3.setGeometry(QtCore.QRect(10, 10, 251, 241))
self.groupBox_3.setObjectName("groupBox_3")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.groupBox_3)
self.plainTextEdit.setGeometry(QtCore.QRect(10, 20, 231, 171))
self.plainTextEdit.setObjectName("plainTextEdit")
self.pushButton_openinfo = QtWidgets.QPushButton(self.groupBox_3)
self.pushButton_openinfo.setGeometry(QtCore.QRect(10, 200, 71, 31))
self.pushButton_openinfo.setObjectName("pushButton_openinfo")
self.pushButton_saveinfo = QtWidgets.QPushButton(self.groupBox_3)
self.pushButton_saveinfo.setGeometry(QtCore.QRect(90, 200, 71, 31))
self.pushButton_saveinfo.setObjectName("pushButton_saveinfo")
self.checkBox_autoline = QtWidgets.QCheckBox(self.groupBox_3)
self.checkBox_autoline.setGeometry(QtCore.QRect(170, 208, 71, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.checkBox_autoline.sizePolicy().hasHeightForWidth())
self.checkBox_autoline.setSizePolicy(sizePolicy)
self.checkBox_autoline.setObjectName("checkBox_autoline")
self.groupBox_style = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_style.setGeometry(QtCore.QRect(10, 260, 251, 171))
self.groupBox_style.setObjectName("groupBox_style")
self.label_block_style = QtWidgets.QLabel(self.groupBox_style)
self.label_block_style.setGeometry(QtCore.QRect(10, 20, 141, 141))
self.label_block_style.setFrameShape(QtWidgets.QFrame.Box)
self.label_block_style.setFrameShadow(QtWidgets.QFrame.Raised)
self.label_block_style.setAlignment(QtCore.Qt.AlignCenter)
self.label_block_style.setObjectName("label_block_style")
self.comboBox_block_size = QtWidgets.QComboBox(self.groupBox_style)
self.comboBox_block_size.setGeometry(QtCore.QRect(190, 60, 51, 30))
self.comboBox_block_size.setObjectName("comboBox_block_size")
self.labe_4 = QtWidgets.QLabel(self.groupBox_style)
self.labe_4.setGeometry(QtCore.QRect(160, 70, 24, 12))
self.labe_4.setAlignment(QtCore.Qt.AlignCenter)
self.labe_4.setObjectName("labe_4")
self.label_block_color = MyLabel(self.groupBox_style)
self.label_block_color.setGeometry(QtCore.QRect(190, 100, 51, 31))
self.label_block_color.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_block_color.setText("")
self.label_block_color.setObjectName("label_block_color")
self.label_4 = QtWidgets.QLabel(self.groupBox_style)
self.label_4.setGeometry(QtCore.QRect(160, 110, 24, 12))
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.label_4.setObjectName("label_4")
self.checkBox_white_block = QtWidgets.QCheckBox(self.groupBox_style)
self.checkBox_white_block.setGeometry(QtCore.QRect(160, 140, 81, 30))
self.checkBox_white_block.setObjectName("checkBox_white_block")
self.labe_8 = QtWidgets.QLabel(self.groupBox_style)
self.labe_8.setGeometry(QtCore.QRect(160, 30, 24, 12))
self.labe_8.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labe_8.setObjectName("labe_8")
self.comboBox_block_shape = QtWidgets.QComboBox(self.groupBox_style)
self.comboBox_block_shape.setGeometry(QtCore.QRect(190, 20, 51, 30))
self.comboBox_block_shape.setObjectName("comboBox_block_shape")
self.comboBox_st_block_size = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_st_block_size.setGeometry(QtCore.QRect(330, 50, 61, 30))
self.comboBox_st_block_size.setCurrentText("")
self.comboBox_st_block_size.setObjectName("comboBox_st_block_size")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(270, 19, 48, 12))
self.label_1.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_1.setObjectName("label_1")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(270, 59, 48, 12))
self.label_5.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_5.setObjectName("label_5")
self.comboBox_error_correct = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_error_correct.setGeometry(QtCore.QRect(330, 10, 61, 30))
self.comboBox_error_correct.setObjectName("comboBox_error_correct")
self.comboBox_edge = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_edge.setGeometry(QtCore.QRect(330, 90, 61, 30))
self.comboBox_edge.setCurrentText("")
self.comboBox_edge.setObjectName("comboBox_edge")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 99, 48, 12))
self.label_3.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_3.setObjectName("label_3")
self.comboBox_border_size = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_border_size.setGeometry(QtCore.QRect(330, 130, 61, 30))
self.comboBox_border_size.setCurrentText("")
self.comboBox_border_size.setObjectName("comboBox_border_size")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(270, 138, 48, 12))
self.label_7.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_7.setObjectName("label_7")
self.label_border_color = MyLabel(self.centralwidget)
self.label_border_color.setGeometry(QtCore.QRect(330, 172, 61, 30))
self.label_border_color.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_border_color.setText("")
self.label_border_color.setObjectName("label_border_color")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(270, 180, 48, 12))
self.label_8.setObjectName("label_8")
self.label_background_color = MyLabel(self.centralwidget)
self.label_background_color.setGeometry(QtCore.QRect(330, 213, 61, 30))
self.label_background_color.setText("")
self.label_background_color.setObjectName("label_background_color")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(580, 520, 161, 21))
self.label_2.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_2.setObjectName("label_2")
self.pushButton_save_set = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_save_set.setGeometry(QtCore.QRect(570, 420, 81, 31))
self.pushButton_save_set.setObjectName("pushButton_save_set")
self.pushButton_open_set = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_open_set.setGeometry(QtCore.QRect(660, 420, 81, 31))
self.pushButton_open_set.setObjectName("pushButton_open_set")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(270, 260, 121, 61))
self.groupBox.setObjectName("groupBox")
self.comboBox_version = QtWidgets.QComboBox(self.groupBox)
self.comboBox_version.setGeometry(QtCore.QRect(10, 20, 51, 30))
self.comboBox_version.setObjectName("comboBox_version")
self.checkBox_version = QtWidgets.QCheckBox(self.groupBox)
self.checkBox_version.setGeometry(QtCore.QRect(70, 28, 47, 16))
self.checkBox_version.setObjectName("checkBox_version")
self.checkBox_bk_color = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_bk_color.setGeometry(QtCore.QRect(270, 220, 59, 16))
self.checkBox_bk_color.setObjectName("checkBox_bk_color")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "二维码生成器"))
self.pushButton_creatqrcode.setText(_translate("MainWindow", "生成"))
self.label_qrcode_img.setText(_translate("MainWindow", "二维码图片预览"))
self.pushButton_saveqrcode.setText(_translate("MainWindow", "保存"))
self.pushButton_about.setText(_translate("MainWindow", "关于"))
self.pushButton_help.setText(_translate("MainWindow", "帮助"))
self.groupBox_center_pic.setTitle(_translate("MainWindow", "嵌入图像"))
self.label_center_picture.setText(_translate("MainWindow", "预览"))
self.labe_2.setText(_translate("MainWindow", "大小"))
self.labe_3.setText(_translate("MainWindow", "圆角"))
self.pushButton_addcenterpic.setText(_translate("MainWindow", "添加"))
self.pushButton_delcenterpic.setText(_translate("MainWindow", "取消"))
self.groupBox_bk_pic.setTitle(_translate("MainWindow", "背景图片"))
self.labe_5.setText(_translate("MainWindow", "对比度"))
self.labe_6.setText(_translate("MainWindow", "透明度"))
self.label_bk_picture.setText(_translate("MainWindow", "预览"))
self.pushButton_addbkpic.setText(_translate("MainWindow", "添加"))
self.pushButton_delbkpic.setText(_translate("MainWindow", "取消"))
self.labe_10.setText(_translate("MainWindow", "模式"))
self.groupBox_3.setTitle(_translate("MainWindow", "二维码信息"))
self.pushButton_openinfo.setText(_translate("MainWindow", "打开"))
self.pushButton_saveinfo.setText(_translate("MainWindow", "保存"))
self.checkBox_autoline.setText(_translate("MainWindow", "自动换行"))
self.groupBox_style.setTitle(_translate("MainWindow", "码块样式"))
self.label_block_style.setText(_translate("MainWindow", "预览"))
self.labe_4.setText(_translate("MainWindow", "大小"))
self.label_4.setText(_translate("MainWindow", "颜色"))
self.checkBox_white_block.setText(_translate("MainWindow", "白色码块"))
self.labe_8.setText(_translate("MainWindow", "形状"))
self.label_1.setText(_translate("MainWindow", "容错级别"))
self.label_5.setText(_translate("MainWindow", "标码大小"))
self.label_3.setText(_translate("MainWindow", "边缘空白"))
self.label_7.setText(_translate("MainWindow", "边框宽度"))
self.label_8.setText(_translate("MainWindow", "边框颜色"))
self.label_2.setText(_translate("MainWindow", "Ver: 3.0.2 狗尾草"))
self.pushButton_save_set.setText(_translate("MainWindow", "保存配置"))
self.pushButton_open_set.setText(_translate("MainWindow", "导入配置"))
self.groupBox.setTitle(_translate("MainWindow", "版本号"))
self.checkBox_version.setText(_translate("MainWindow", "自动"))
self.checkBox_bk_color.setText(_translate("MainWindow", "背景色"))
self._init_widget() #初始化控件
def _init_widget(self): #自定义初始化控件
#初始化变量
self.__QrcodeImg = None #定义私有成员二维码图片变量,保存生成的二维码图片
self.__CenterImg = None #定义私有成员图片文件名变量,保存添加图片的绝对路径文件名
self.__CenterImg_Change = None
self.__BkImg = None
self.__BkImg_Change = None
self.__BlockColor = (0,0,0)
self.__BorderColor = (0,170,0)
self.__BkColor = (255,255,255)
#初始化二维码主要参数
self.comboBox_error_correct.addItems(['L 7%', 'M 15%', 'Q 25%', 'H 30%']) #容错级别
#self.comboBox_error_correct.setCurrentIndex(1) #设置默认为 M
for i in range(10, 81):self.comboBox_st_block_size.addItem(str(i) + ' px') #标码大小
self.comboBox_st_block_size.setCurrentIndex(20) #标准码块的大小
self.comboBox_edge.addItem('无')
for i in range(1, 41): self.comboBox_edge.addItem(str(i) + '%') #边缘空白
self.comboBox_border_size.addItem('无')
for i in range(3, 51): self.comboBox_border_size.addItem(str(i/10) + '%') #边框宽度
self.label_border_color.setStyleSheet('background-color:rgb(0,170,0)') #边框默认颜色
self.label_border_color.DoubleClicked.connect(self.SetBorderColor) #边框颜色标签双击事件
self.checkBox_bk_color.setChecked(True) #背景颜色复选框默认选中
self.checkBox_bk_color.stateChanged.connect(self.SetBkColorState) #背景颜色复选框状态改变事件
self.label_background_color.setStyleSheet('background-color:rgb(255,255,255)') #背景颜色
self.label_background_color.DoubleClicked.connect(self.SetBackgroundColor) #背景颜色标签双击事件
self.checkBox_version.setChecked(True) #版本号自动复选框默认选中
self.checkBox_version.stateChanged.connect(self.SetVersion) #版本号复选框状态改变事件
for i in range(1, 41): self.comboBox_version.addItem(str(i)) #版本号
self.comboBox_version.setEnabled(False) #版本号下拉框默认不可用
#主功能按钮
self.pushButton_creatqrcode.clicked.connect(self.CreatQrcode) #生成二维码
self.pushButton_saveqrcode.clicked.connect(self.SaveQrcode) #保存二维码
self.pushButton_save_set.clicked.connect(self.SaveSet) #保存配置
self.pushButton_open_set.clicked.connect(self.OpenSet) #导入配置
self.pushButton_help.clicked.connect(self.Help) #帮助
self.pushButton_about.clicked.connect(self.About) #关于
#二维码文本信息:打开、保存、自动换行
self.pushButton_openinfo.clicked.connect(self.OpenFile) #打开信息
self.pushButton_saveinfo.clicked.connect(self.SaveFile) #保存信息
self.plainTextEdit.setLineWrapMode(True) #允许自动换行
self.checkBox_autoline.setChecked(True) #自动换行复选框默认选中
self.checkBox_autoline.stateChanged.connect(self.AutoLine) #自动换行复选框状态变化事件
#码块样式
self.comboBox_block_shape.addItems(['方形', '圆形', '菱形', '横纹', '竖纹', '十字']) #码块的形状
self.comboBox_block_shape.currentIndexChanged.connect(self.ChangeStyle) #码块形状下拉框事件处理
for i in range(100, 9, -10): self.comboBox_block_size.addItem(str(i) + '%') #码块的大小
self.label_block_color.setStyleSheet('background-color:rgb(0,0,0)') #码块的颜色
self.label_block_color.DoubleClicked.connect(self.SetBlockColor) #码块颜色标签双击事件
self.comboBox_block_size.currentIndexChanged.connect(self.ChangeStyle) #码块大小下拉框事件处理
self.checkBox_white_block.setChecked(False) #白色码块,默认不选中
self.checkBox_white_block.stateChanged.connect(self.ChangeStyle) #白色码块复选框状态改变事件
self.ChangeStyle() #调用样式设置函数一次,在预览中显示
#嵌入图像
self.comboBox_pic_size.setEnabled(False) #图像大小下拉框默认不可用
self.comboBox_pic_circle.setEnabled(False) #图像圆角下拉框默认不可用
self.pushButton_addcenterpic.clicked.connect(self.AddCenterPic) #添加图像按钮事件
self.pushButton_delcenterpic.clicked.connect(self.DelCenterPic) #取消图像按钮事件
for i in range(10, 31):self.comboBox_pic_size.addItem(str(i) + '%') #图像大小下拉框数值初始化
self.comboBox_pic_size.setCurrentIndex(5) #图像大小默认15%
self.comboBox_pic_circle.addItem('无')
for i in range(10, 101, 10):self.comboBox_pic_circle.addItem(str(i) + '%') #圆角下拉框数值初始化
self.comboBox_pic_circle.currentIndexChanged.connect(self.SetPicCirle) #圆角下拉框事件处理
#背景图片
self.comboBox_bk_tran.setEnabled(False) #背景透明度默认不可用
self.comboBox_bk_contrast.setEnabled(False) #背景对比度默认不可用
self.comboBox_bk_mode.setEnabled(False) #背景模式默认不可用
self.comboBox_bk_mode.addItems(['拉伸','裁剪']) #模式下拉框数值
for i in range(0, 91, 10): self.comboBox_bk_tran.addItem(str(i) + '%') #透明度下拉框数值
self.comboBox_bk_contrast.addItems(['1.0', '0.9', '0.8', '0.7', '0.6', '0.5', '0.4', '0.3', '0.2', '0.1']) #添加背景对比度下拉框数值
self.pushButton_addbkpic.clicked.connect(self.AddBkPic) #添加背景按钮事件
self.pushButton_delbkpic.clicked.connect(self.DelBkPic) #取消背景按钮事件
self.comboBox_bk_mode.currentIndexChanged.connect(self.UpdateBkPic) #模式下拉框事件处理
self.comboBox_bk_tran.currentIndexChanged.connect(self.UpdateBkPic) #透明度下拉框事件处理
self.comboBox_bk_contrast.currentIndexChanged.connect(self.UpdateBkPic) #对比度下拉框事件处理
def SetBorderColor(self): #设置边框颜色
c='#'
for i in self.__BorderColor: #把颜色元组转换成16进制的字符串
c += str(hex(i))[-2:].replace('x', '0')
col = QColorDialog.getColor(QColor(c)) #调用颜色选择对话框,默认为之前的颜色
if col.isValid(): #把选中的颜色进行通道分离
r = int(col.name()[1:3], 16)
g = int(col.name()[3:5], 16)
b = int(col.name()[5:7], 16)
self.__BorderColor = (r, g, b)
self.label_border_color.setStyleSheet('background-color:' + col.name())
def SetBackgroundColor(self): #设置背景颜色
c = '#'
for i in self.__BkColor:
c += str(hex(i))[-2:].replace('x', '0')
col = QColorDialog.getColor(QColor(c))
if col.isValid():
r = int(col.name()[1:3], 16)
g = int(col.name()[3:5], 16)
b = int(col.name()[5:7], 16)
self.__BkColor = (r, g, b)
self.label_background_color.setStyleSheet('background-color:' + col.name())
def SetBkColorState(self): #背景颜色复选框状态判断
if self.checkBox_bk_color.isChecked():
self.label_background_color.setEnabled(True)
else:
self.label_background_color.setEnabled(False)
def SetVersion(self): #设置版本
if self.checkBox_version.isChecked():
self.comboBox_version.setEnabled(False)
else:
self.comboBox_version.setEnabled(True)
def OpenFile(self): #打开信息
filename, filetype = QFileDialog.getOpenFileName(self, "选取文件", "", "文本文件 (*.txt)")
if filename:
rf = open(filename, 'r')
self.plainTextEdit.setPlainText('')
for line in rf.readlines():
line = line.strip()
self.plainTextEdit.appendPlainText(line)
rf.close()
def SaveFile(self): #保存信息
filename, filetype = QFileDialog.getSaveFileName(self, "文件保存", "", "文本文件 (*.txt)")
if filename:
wf = open(filename, 'w')
wf.write(self.plainTextEdit.toPlainText())
wf.close()
def AutoLine(self): #自动换行
if self.checkBox_autoline.isChecked():
self.plainTextEdit.setLineWrapMode(True)
else:
self.plainTextEdit.setLineWrapMode(False)
def Help(self): #帮助
MyDialog = QtWidgets.QDialog()
ui = ihelp.Ui_Dialog_help()
ui.setupUi(MyDialog)
MyDialog.show()
MyDialog.exec_()
def About(self): #关于
QMessageBox.about(self, '关于二维码生成器', idata.abouttxt)
def CreatQrcode(self): #生成二维码
if self.plainTextEdit.toPlainText() == '': #判断文本信息框是否有内容
QMessageBox.information(self, '提示', '请先输入文本信息!', QMessageBox.Ok, QMessageBox.Ok)
self.plainTextEdit.setFocus()
return
error_correct = [qrcode.ERROR_CORRECT_L, qrcode.ERROR_CORRECT_M, qrcode.ERROR_CORRECT_Q, qrcode.ERROR_CORRECT_H]
Num_error_correct = error_correct[self.comboBox_error_correct.currentIndex()]
if self.checkBox_version.isChecked(): #根据版本号是否自动设置参数
Num_fit = True
ver = None
else:
Num_fit = False
ver = self.comboBox_version.currentIndex() + 1
try:
qr = qrcode.QRCode(
version = ver, #编码版本,即大小1-40,自动None
error_correction = Num_error_correct, #设置容错率
box_size = 1,
border = 0, #设置边框宽度
)
qr.add_data(self.plainTextEdit.toPlainText())
qr.make(fit = Num_fit)
data = qr.get_matrix() #二维码数据列表
except:
QMessageBox.information(self, '错误', '文本信息内容过多或者版本号过低,' +
'请重新输入文本信息或者选择\n更高的版本号(建议设置为自动,以适配最合适的版本号)。',
QMessageBox.Ok, QMessageBox.Ok
)
return
self.comboBox_version.setCurrentIndex(qr.version - 1) #把版本号下拉框数值设置为当前版本号
self.label_qrcode_img.setText('正在生成二维码,请稍候...')
QApplication.processEvents() #实时刷新界面,使标签文本立即显示
img = self.CreatImg(data) #调用二维码图像生成函数
qrsize, qrsize = img.size
if self.__CenterImg_Change != None: #把图像嵌入到二维码图片中心
sz = int(qrsize * (self.comboBox_pic_size.currentIndex() + 10) / 100)
cimg = self.__CenterImg_Change.resize((sz, sz))
sz = int((qrsize - sz) / 2)
img.paste(cimg, (sz, sz), cimg)
if self.__BkImg_Change != None: #如果选择了背景图片,则把背景变量设为背景图片
bk = self.__BkImg_Change.resize((qrsize, qrsize))
elif self.checkBox_bk_color.isChecked(): #如果没有选择背景图片,但背景颜色下拉框被选中,则根据背景颜色创建一个背景变量
bk = Image.new('RGBA', (qrsize, qrsize), self.__BkColor)
else: #如果无背景图片,且无背景色,则创建一个透明背景
bk = Image.new('RGBA', (qrsize , qrsize), (255, 255, 255, 0))
sz = int(self.comboBox_edge.currentIndex() * qrsize / 100) #根据边缘空白计算图像尺寸
img = img.resize((qrsize - 2 * sz, qrsize - 2 * sz)) #缩小二维码图像尺寸
bk.paste(img, (sz, sz), img) #把二维码图像粘贴到背景上
img = bk #把新的二维码图片重新赋值给img变量
if self.comboBox_border_size.currentIndex() > 0: #画边框
bk = Image.new('RGBA', (qrsize, qrsize), (255, 255, 255, 0)) #创建一个二维码图像大小的空白背景
sz = int((self.comboBox_border_size.currentIndex()+2) * qrsize / 1000) #根据边框下拉框数值计算边框尺寸
#在四个边画填充的矩形
bk_draw = ImageDraw.Draw(bk)
bk_draw.rectangle((0, 0, qrsize - 1, sz - 1), fill = self.__BorderColor)
bk_draw.rectangle((0, 0, sz - 1, qrsize - 1), fill = self.__BorderColor)
bk_draw.rectangle((qrsize - sz - 1, 0, qrsize - 1, qrsize - 1), fill = self.__BorderColor)
bk_draw.rectangle((0, qrsize - sz - 1, qrsize - 1, qrsize - 1), fill = self.__BorderColor)
img = img.resize((qrsize - 2 * sz, qrsize - 2 * sz)) #根据边框尺寸缩小二维码图片
bk.paste(img, (sz, sz), img) #把二维码图像粘贴到背景上
img = bk #把新的二维码图片重新赋值给img变量
self.__QrcodeImg = img #把最终的二维码图片赋值给全局变量用作保存文件
qimg = ImageQt.ImageQt(img) #把PIL的Image图像转换成PYQT5的Qimage图像
qimg = qimg.scaled(self.label_qrcode_img.width() - 10, self.label_qrcode_img.height() - 10) #设置图像大小与label大小一致
self.label_qrcode_img.setPixmap(QPixmap.fromImage(qimg)) #把Qimage图像转换成QPixmap图像并显示在label上
def SaveQrcode(self): #保存二维码
if self.__QrcodeImg == None:
QMessageBox.information(self, '提示', '请先生成二维码!', QMessageBox.Ok, QMessageBox.Ok)
return
else:
filename, filetype = QFileDialog.getSaveFileName(self, "文件保存", "", "图像文件 (*.png);;图像文件 (*.jpg)")
if filename:
if filetype =='图像文件 (*.jpg)': #如果保存成jpe文件,则把图像转变成RGB格式
img = self.__QrcodeImg.convert('RGB')
img.save(filename, 'JPEG')
else:
self.__QrcodeImg.save(filename, 'PNG')
def SetBlockColor(self): #设置码块颜色
c = '#'
for i in self.__BlockColor:
c += str(hex(i))[-2:].replace('x', '0')
col = QColorDialog.getColor(QColor(c))
if col.isValid():
r = int(col.name()[1:3], 16)
g = int(col.name()[3:5], 16)
b = int(col.name()[5:7], 16)
self.__BlockColor = (r, g, b)
self.label_block_color.setStyleSheet('background-color:' + col.name())
self.ChangeStyle()
def ChangeStyle(self): #刷新样式标签
img = self.CreatImg(idata.qrdata, 'style') #调用二维码生成函数生成一个样式二维码图片
qimg = ImageQt.ImageQt(img) #把PIL的Image图像转换成PYQT5的Qimage图像
qimg = qimg.scaled(self.label_block_style.width() - 10, self.label_block_style.height() - 10) #设置图像大小与label大小一致
self.label_block_style.setPixmap(QPixmap.fromImage(qimg)) #把Qimage图像转换成QPixmap图像并显示在label上
def CreatImg(self, data = [], flag = None): #根据列表数据创建二维码标准图片
if flag == 'style': #如果flag为'style',则说明是码块样式设置调用的此函数
boxsize = 10
else:
boxsize = self.comboBox_st_block_size.currentIndex() + 10 #把标准码块大小设置成下拉框的数值
bs2 = 2*boxsize
#画一个自定义颜色的标准码块和一个自定义大小的码块
bblock0 = Image.new('RGBA', (bs2,bs2), (255,255,255,0))
bblock0_draw = ImageDraw.Draw(bblock0)
shape = self.comboBox_block_shape.currentText()
si = int((100 - self.comboBox_block_size.currentIndex()*10) * boxsize / 100)
if shape == '方形':
bblock0_draw.rectangle((0, 0, bs2 - 1,bs2 - 1), fill = self.__BlockColor)
w = si
h = si
elif shape == '圆形':
bblock0_draw.ellipse((0, 0, bs2 - 1,bs2 - 1), fill = self.__BlockColor)
w = si
h = si
elif shape == '菱形':
bblock0_draw.polygon((boxsize - 1, 0, 0, boxsize - 1, boxsize - 1, bs2 - 1, bs2 - 1, boxsize - 1), fill = self.__BlockColor)
w = si
h = si
elif shape == '横纹':
bblock0_draw.rectangle((0, 0, bs2 - 1, bs2 - 1), fill = self.__BlockColor)
w = boxsize
h = si
elif shape == '竖纹':
bblock0_draw.rectangle((0, 0, bs2 - 1, bs2 - 1), fill = self.__BlockColor)
w = si
h = boxsize
elif shape == '十字':
bblock0_draw.rectangle((0, int(bs2 * 0.4), bs2 - 1, int(bs2 * 0.6)) ,fill = self.__BlockColor)
bblock0_draw.rectangle((int(bs2 * 0.4), 0, int(bs2 * 0.6), bs2 - 1), fill = self.__BlockColor)
w = si
h = si
bblock0=bblock0.resize((boxsize, boxsize))
bblock0_change=bblock0.resize((w, h))
bblock=Image.new('RGBA', (boxsize, boxsize), (255, 255, 255, 0))
w0 = int((boxsize - w) / 2)
h0 = int((boxsize - h) / 2)
bblock.paste(bblock0_change, (w0, h0))
#画一个白色的标准码块和一个自定义大小的码块
if self.checkBox_white_block.isChecked():
wblock0 = Image.new('RGBA', (bs2, bs2), (255, 255, 255, 0))
wblock0_draw = ImageDraw.Draw(wblock0)
if shape == '方形' or shape == '横纹' or shape == '竖纹':
wblock0_draw.rectangle((0, 0, bs2 - 1,bs2 - 1), fill = (255, 255, 255))
elif shape == '圆形':
wblock0_draw.ellipse((0, 0, bs2 - 1, bs2 - 1), fill = (255, 255, 255))
elif shape == '菱形':
wblock0_draw.polygon((boxsize - 1, 0, 0, boxsize - 1, boxsize - 1, bs2 - 1, bs2 - 1, boxsize - 1), fill = (255, 255, 255))
elif shape == '十字':
wblock0_draw.rectangle((0, int(bs2 * 0.4), bs2 - 1, int(bs2 * 0.6)), fill = (255, 255, 255))
wblock0_draw.rectangle((int(bs2 * 0.4), 0, int(bs2 * 0.6), bs2 - 1), fill = (255, 255, 255))
wblock0 = wblock0.resize((boxsize, boxsize))
wblock0_change = wblock0.resize((w, h))
wblock = Image.new('RGBA', (boxsize, boxsize), (255, 255, 255, 0))
wblock.paste(wblock0_change, (w0, h0))
#把标准码块和自定义大小的码块粘贴到一个透明背景上
num = len(data)
size = num * boxsize
img = Image.new('RGBA', (size, size), (255, 255, 255, 0))
for h in range(num):
for w in range(num):
wb = w * boxsize
wh = h * boxsize
if data[h][w]:
if (w < 7 and h < 7) or (w > num - 8 and h < 7) or (w <7 and h > num - 8): #判断定位图形的位置
img.paste(bblock0, (wb, wh))
else:
img.paste(bblock, (wb, wh))
elif self.checkBox_white_block.isChecked():
if (w < 7 and h < 7) or (w > num - 8 and h < 7) or (w < 7 and h > num - 8): #判断定位图形的位置
img.paste(wblock0, (wb, wh))
else:
img.paste(wblock, (wb, wh))
return img
def AddCenterPic(self): #嵌入图片
imgname, imgtype = QFileDialog.getOpenFileName(self, "选取图片", "", "图像文件 (*.png , *.jpg , *.bmp)")
if imgname:
self.comboBox_pic_size.setEnabled(True)
self.comboBox_pic_circle.setEnabled(True)
img = Image.open(imgname)
img = img.resize((1000, 1000))
img = img.convert('RGBA')
if imgname[-3:].upper() == 'PNG': #如果打开的图片是png格式,则添加一个白色不透明的背景
self.__CenterImg = Image.new('RGBA', img.size, (255, 255, 255, 255))
self.__CenterImg.paste(img, (0, 0), img)
else:
self.__CenterImg = img
self.__CenterImg_Change = self.__CenterImg
self.SetPicCirle()
def DelCenterPic(self): #删除嵌入图片
self.label_center_picture.setText('预览')
self.__CenterImg = None
self.__CenterImg_Change = None
self.comboBox_pic_size.setEnabled(False)
self.comboBox_pic_circle.setEnabled(False)
def SetPicCirle(self): #对嵌入图片进行圆角处理
wh = 1000
pic = self.__CenterImg
r = int(wh * self.comboBox_pic_circle.currentIndex() / 20)
circle=Image.new('L', (r * 2,r * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, r * 2, r * 2), fill = 255)
alpha = Image.new('L', (wh, wh), 255)
alpha.paste(circle.crop((0, 0, r, r)), (0, 0))
alpha.paste(circle.crop((0, r, r, r * 2)), (0, wh - r))
alpha.paste(circle.crop((r, 0, r * 2, r)), (wh - r, 0))
alpha.paste(circle.crop((r, r, r * 2, r * 2)), (wh - r, wh - r))
#先对图像进行圆角处理,然后分三次粘贴到不同的背景上,达到边框立体的效果
pic.putalpha(alpha)
pic = pic.resize((wh - 80, wh - 80))
pic0 = Image.new('RGBA', (wh, wh), (240, 240, 240, 255))
pic0.putalpha(alpha)
pic0 = pic0.resize((wh - 48, wh - 48))
pic0.paste(pic, (16, 16), pic)
pic = pic0
pic0 = Image.new('RGBA', (wh, wh), (150, 150, 150, 255))
pic0.putalpha(alpha)
pic0 = pic0.resize((wh - 32, wh - 32))
pic0.paste(pic, (8, 8), pic)
pic = pic0
pic0 = Image.new('RGBA', (wh, wh), (220, 220, 220, 255))
pic0.putalpha(alpha)
pic0.paste(pic, (16, 16), pic)
self.__CenterImg_Change = pic0
pic0 = ImageQt.ImageQt(pic0)
pic0 = pic0.scaled(self.label_center_picture.width() - 5, self.label_center_picture.height() - 5)
self.label_center_picture.setPixmap(QPixmap.fromImage(pic0))
def AddBkPic(self): #添加背景图片
imgname, imgtype = QFileDialog.getOpenFileName(self,"选取图片","","图像文件 (*.png , *.jpg , *.bmp)")
if imgname:
self.comboBox_bk_mode.setEnabled(True)
self.comboBox_bk_tran.setEnabled(True)
self.comboBox_bk_contrast.setEnabled(True)
self.checkBox_bk_color.setEnabled(False)
self.label_background_color.setEnabled(False)
self.__BkImg=Image.open(imgname)
self.UpdateBkPic()
def UpdateBkPic(self): #更新背景图片预览
img = self.__BkImg
img.convert('RGBA')
w, h = img.size
if self.comboBox_bk_mode.currentText() == '拉伸':
sz = max([w, h])
img = img.resize((sz, sz))
else:
sz = min([w, h])
w = (w - sz) / 2
h = (h - sz) / 2
img = img.crop((w, h, w + sz, h + sz))
alpha = Image.new('L', img.size, int((10 -self.comboBox_bk_tran.currentIndex()) * 25.5))
img.putalpha(alpha)
bk = Image.new('RGBA', img.size, (255, 255, 255, 255))
bk.paste(img, (0, 0), img) #把有透明度的图片贴到一张白色背景上
img = bk
img = ImageEnhance.Contrast(img)
img = img.enhance(1 - self.comboBox_bk_contrast.currentIndex() / 10)
self.__BkImg_Change = img
img = ImageQt.ImageQt(img)
img = img.scaled(self.label_bk_picture.width(), self.label_bk_picture.height())
self.label_bk_picture.setPixmap(QPixmap.fromImage(img))
def DelBkPic(self): #删除背景图片
self.label_bk_picture.setText('背景预览')
self.__BkImg = None
self.__BkImg_Change = None
self.comboBox_bk_mode.setEnabled(False)
self.comboBox_bk_tran.setEnabled(False)
self.comboBox_bk_contrast.setEnabled(False)
self.checkBox_bk_color.setEnabled(True)
self.label_background_color.setEnabled(True)
def show_MainWindow(): #创建窗口
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) #控件自适应高分辨率屏幕
app = QtWidgets.QApplication(sys.argv)
font = QFont("宋体") #字体适应
pointsize = font.pointSize() #高分辨率
font.setPixelSize(pointsize) #屏幕
app.setFont(font) #必要的字体设置
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
translator = QtCore.QTranslator() #实例化翻译类
translator.load('source/qt_zh_CN.qm') #加载翻译文件
app.installTranslator(translator) #给程序安装翻译文件
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
show_MainWindow()