[题目三]密码验证:
(1)模拟登陆界面,判别用户名和密码,给出合适的提示,如果超过三次,锁定输入。用代替密码;或者最新输入显示,前面的变成;安全性措施。
(2)同时添加如下图的加强验证(京东)。(3)在触动滚动条时再出现空缺位置。
扩展:其他或自己设计一个加强型的验证,目的是区分人工与机器登录。
DL.py
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from DL__UI import *
from zc_ui import *
from random import randint
import os,random
from PIL import Image, ImageDraw, ImageFont, ImageFilter
class ZC(QMainWindow, Ui_Form):
def __init__(self,userInfo):
"""
userInfo用来放注册时的变量
注册子窗口
继承布局文件,实现进程控制的功能
其中userlist可以用来放用户列表
connecter为按钮监听器
"""
super(ZC, self).__init__()
self.setupUi(self)
self.connecter()
self.userList=userInfo
def connecter(self):
self.pushButton.clicked.connect(self._pushButton)
def _pushButton(self):
"""
注册确认按钮出发函数,包括一些
合法性的检测,用户信息的输入与记录
"""
temp={}
userName=self.lineEdit.text()
mail=self.lineEdit_2.text()
pwd1=self.lineEdit_3.text()
pwd2=self.lineEdit_4.text()
if str(userName)=="":
QMessageBox.warning(self, "NOTICE", "用户名不能为空", QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.Yes)
if str(mail) == "":
QMessageBox.warning(self, "NOTICE", "邮箱不能为空", QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.Yes)
if str(pwd1)=="":
QMessageBox.warning(self, "NOTICE", "密码不能为空", QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.Yes)
if pwd1!=pwd2:
QMessageBox.warning(self,"NOTICE","两次输入密码不一致",QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.Yes)
self.lineEdit_3.clear()
self.lineEdit_4.clear()
else:
temp[str(userName)]=[str(mail),str(pwd1)]
self.userList.append(temp)
QMessageBox.information(self, "NOTICE", "注册成功", QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.Yes)
self.close()
class DL(QMainWindow, Ui_MainWindow):
def __init__(self):
"""
登录的主窗口
继承布局文件,实现进程控制的功能
其中timeout为登录错误次数上限
list放入用户的信息列表
connecter为按钮监听器
"""
super(DL, self).__init__()
self.list=[{"admin":["","password"]}]
self.setupUi(self)
self.timeOut=2
self.verfyCode=self.gene_code()
self.connecter()
def connecter(self):
self.pushButton.clicked.connect(self._pushButton)
self.pushButton_2.clicked.connect(self._pushButton_2)
self.pushButton_3.clicked.connect(self._pushButton_3)
def _pushButton_2(self):
"""
注册按钮触发子窗口
"""
self.zc=ZC(userInfo=self.list)
self.zc.show()
def _pushButton(self):
"""
登录信息的合法性检测,包括弹窗
:return: 返回登录结果True or False
"""
_translate = QtCore.QCoreApplication.translate
print(self.list)
name=str(self.lineEdit.text())
pwd=str(self.lineEdit_2.text())
code=str(self.lineEdit_3.text())
if self.timeOut>=0:
if self.verfyCode==code:
if pwd==self.list[0][name][1]:
print(pwd,self.list[0][name][1])
QMessageBox.information(self,"NOTICE","登录成功",QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.Yes)
return True
else:
QMessageBox.warning(self,"NOTICE","用户名不存在或密码错误",QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.Yes)
else:
QMessageBox.information(self,"NOTICE","验证码错误",QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.Yes)
self.verfyCode=self.gene_code()
self.timeOut-=1
return False
else:
QMessageBox.warning(self, "NOTICE", "错误三次,用户已锁定", QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.Yes)
self.label_4.setText(" 用户已锁定")
self.update()
return False
def _pushButton_3(self):
"""
验证码更新,即换一个验证码
"""
self.verfyCode=self.gene_code()
print(self.verfyCode)
def gene_line(self,draw, width, height, linecolor):
"""
:param draw: 传入的handle用来对图片进行操作
:param width: 图片的宽度
:param height: 图片的高度
:param linecolor: 画笔的颜色
"""
# random.randint(a, b)用于生成一个指定范围内的证书,其中第一个参数a是上限,第二个参数b是下限,生成的随机数n:a<=n<=b
begin = (random.randint(0, width), random.randint(0, height))
end = (random.randint(0, width), random.randint(0, height))
# 在图像上画线,参数值为线的起始和终止位置坐标[(x, y), (x, y)]和线的填充颜色
draw.line([begin, end], fill=linecolor)
def gene_code(self,size=(80, 30),bgcolor=(255, 255, 255),number=4, draw_line=True, fontcolor=(0, 0, 255)):
"""
:param size: 图片的尺寸
:param bgcolor: 背景颜色
:param number: 字符串长度
:param draw_line: 是否生成扰乱线
:param fontcolor: 字体的颜色
:return: 返回验证码的字符串
"""
# 宽和高
width, height = size
# 创建图片, 'RGBA'表示4*8位像素,真彩+透明通道
image = Image.new('RGBA', (width, height), bgcolor)
# 创建画笔,创建可用于绘制给定图像的对象
draw = ImageDraw.Draw(image)
font_path = 'calibri.ttf'
font = ImageFont.truetype(font_path, 25)
# 随机生成想要的字符串
text=str(randint(0,9))+str(randint(0,9))+str(randint(0,9))+str(randint(0,9))
# 返回给定文本的宽度和高度,返回值为2元组
font_width, font_height = font.getsize(text)
# 填充字符串,参数分别是:文本的左上角坐标,文本内容,字体,文本的填充颜色
draw.text(((width - font_width) / (number-3), (height - font_height) / (number-3)),text,fill=fontcolor)
linecolor=(0,0,255)
if draw_line:
# 计算要画的线的条数
line_count = random.randint(1,5)
for i in range(line_count):
self.gene_line(draw, width, height, linecolor)
# 创建扭曲,transform(size, method, data) 其中第一个参数是尺寸大小, Image.AFFINE表示仿射变化
# 第三个参数是转换方法的额外数据, Image.BILINEAR是线性插值法
image = image.transform((width, height), Image.AFFINE, (1, -0.3, 0, -0.1, 1, 0), Image.BILINEAR)
# 滤镜,边界加强,ImageFilter.EDGE_ENHANCE_MORE为深度边缘增强滤波,会使得图像中边缘部分更加明显。
image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)
# 保存验证码图片
image.save('idencode.png')
self.label_5.setPixmap(QtGui.QPixmap('idencode.png'))
self.update()
return text
def main():
app = QApplication(sys.argv)
w = DL()
w.show()
sys.exit(app.exec_())
main()
DL_UI.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(302, 337)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayout = QtWidgets.QFormLayout(self.centralwidget)
self.formLayout.setObjectName("formLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(24)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_5.addWidget(self.pushButton_3)
self.label_5 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.horizontalLayout_5.addWidget(self.label_5)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.label_6 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.horizontalLayout_3.addWidget(self.label_6)
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_3.addWidget(self.lineEdit_3)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(14)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_4.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(14)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_4.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.formLayout.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 302, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_4.setText(_translate("MainWindow", " 用户登录"))
self.label.setText(_translate("MainWindow", "用户名"))
self.label_2.setText(_translate("MainWindow", "密码 "))
self.pushButton_3.setText(_translate("MainWindow", "换一个"))
self.label_5.setText(_translate("MainWindow", " 图片"))
self.label_6.setText(_translate("MainWindow", "验证码"))
self.pushButton.setText(_translate("MainWindow", "登录"))
self.pushButton_2.setText(_translate("MainWindow", "注册"))
zc_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(497, 393)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(130, 80, 246, 198))
self.widget.setObjectName("widget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(20)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout_3.addWidget(self.label)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.verticalLayout_2.addWidget(self.label_3)
self.label_4 = QtWidgets.QLabel(self.widget)
self.label_4.setObjectName("label_4")
self.verticalLayout_2.addWidget(self.label_4)
self.label_5 = QtWidgets.QLabel(self.widget)
self.label_5.setObjectName("label_5")
self.verticalLayout_2.addWidget(self.label_5)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.lineEdit_3 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.verticalLayout.addWidget(self.lineEdit_3)
self.lineEdit_4 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_3.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_4.setEchoMode(QtWidgets.QLineEdit.Password)
self.verticalLayout.addWidget(self.lineEdit_4)
self.horizontalLayout.addLayout(self.verticalLayout)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout_3.addWidget(self.pushButton)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", " 用户注册"))
self.label_2.setText(_translate("Form", "用户名"))
self.label_3.setText(_translate("Form", "邮箱"))
self.label_4.setText(_translate("Form", "密码"))
self.label_5.setText(_translate("Form", "密码确认"))
self.pushButton.setText(_translate("Form", "确认注册"))