初学Python做练习,利用了pyqt5、qrcode、pillow库。学Python太难了,越学感觉不知道的东西越多。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\Desktop\Pythoh\二维码生成器v3.0\qrcode.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# WARNING! All changes made in this file will be lost!
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.1 狗尾草"))
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)
for i in range(10,51):self.comboBox_st_block_size.addItem(str(i)+' px') #标码大小
self.comboBox_st_block_size.setCurrentIndex(10)
self.comboBox_edge.addItem('无')
for i in range(1,21):self.comboBox_edge.addItem(str(i)+'%')
self.comboBox_border_size.addItem('无')
for i in range(1,11):self.comboBox_border_size.addItem(str(i/2)+'%')
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_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')#.upper()
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 SaveSet(self): #保存当前配置
QMessageBox.information(self,'提示','抱歉,此功能不能使用!',QMessageBox.Ok,QMessageBox.Ok)
def OpenSet(self): #打开配置文件
QMessageBox.information(self,'提示','抱歉,此功能不能使用!',QMessageBox.Ok,QMessageBox.Ok)
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
if self.comboBox_border_size.currentIndex()>0: #画边框
bk=Image.new('RGBA',(qrsize,qrsize),(255,255,255,0))
sz=int(self.comboBox_border_size.currentIndex()*qrsize/200)
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=bk
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)':
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')#.upper()
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 CreatImg(self,data=[],flag=None): #根据列表数据创建二维码标准图片
if 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 hnum-8 and h<7) or (wnum-8): #判断定位图形的位置
img.paste(bblock0,(wb,wh))
else:
img.paste(bblock,(wb,wh))
elif self.checkBox_white_block.isChecked():
if (w<7 and hnum-8 and h<7) or (wnum-8): #判断定位图形的位置
img.paste(wblock0,(wb,wh))
else:
img.paste(wblock,(wb,wh))
return img
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 AddCenterPic(self):
imgname, imgtype = QFileDialog.getOpenFileName(self,"选取图片","","图像文件 (*.png , *.jpg , *.jpeg , *.bmp)")
if imgname:
self.comboBox_pic_size.setEnabled(True)
self.comboBox_pic_circle.setEnabled(True)
img = Image.open(imgname)
img = img.resize((500,500))
self.__CenterImg=Image.new('RGBA',img.size,(255,255,255,255))
self.__CenterImg.paste(img,(0,0))
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 = 500
pic = self.__CenterImg
pic0 = Image.new('RGBA', (wh,wh), (220,220,220,255))
r = int(25*self.comboBox_pic_circle.currentIndex())
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)
pic0.putalpha(alpha)
sz=475
pic=pic.resize((sz,sz))
sz=int((wh-sz)/2)
pic0.paste(pic,(sz,sz),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 , *.jpeg , *.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)
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()