实现功能:
""" 1.给不同网站设置密码 2.密码组成方式由 大写,小写,数字,标点符号类别组成 3.生成txt文件保存 """
1.gui设计:
前端生成py代码:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'passwd.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Form(object):def setupUi(self, Form):Form.setObjectName("Form")Form.resize(400, 259)self.widget = QtWidgets.QWidget(Form)self.widget.setGeometry(QtCore.QRect(10, 30, 371, 201))self.widget.setObjectName("widget")self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)self.verticalLayout.setContentsMargins(0, 0, 0, 0)self.verticalLayout.setObjectName("verticalLayout")self.horizontalLayout = QtWidgets.QHBoxLayout()self.horizontalLayout.setObjectName("horizontalLayout")self.label = QtWidgets.QLabel(self.widget)self.label.setObjectName("label")self.horizontalLayout.addWidget(self.label)self.lineEdit_site = QtWidgets.QLineEdit(self.widget)self.lineEdit_site.setObjectName("lineEdit_site")self.horizontalLayout.addWidget(self.lineEdit_site)self.verticalLayout.addLayout(self.horizontalLayout)self.horizontalLayout_2 = QtWidgets.QHBoxLayout()self.horizontalLayout_2.setObjectName("horizontalLayout_2")self.checkBox_upper = QtWidgets.QCheckBox(self.widget)self.checkBox_upper.setObjectName("checkBox_upper")self.horizontalLayout_2.addWidget(self.checkBox_upper)self.checkBox_lower = QtWidgets.QCheckBox(self.widget)self.checkBox_lower.setObjectName("checkBox_lower")self.horizontalLayout_2.addWidget(self.checkBox_lower)self.checkBox_number = QtWidgets.QCheckBox(self.widget)self.checkBox_number.setObjectName("checkBox_number")self.horizontalLayout_2.addWidget(self.checkBox_number)self.checkBox_4 = QtWidgets.QCheckBox(self.widget)self.checkBox_4.setObjectName("checkBox_4")self.horizontalLayout_2.addWidget(self.checkBox_4)self.verticalLayout.addLayout(self.horizontalLayout_2)self.pushButton = QtWidgets.QPushButton(self.widget)self.pushButton.setObjectName("pushButton")self.verticalLayout.addWidget(self.pushButton)self.lineEdit_result = QtWidgets.QLineEdit(self.widget)self.lineEdit_result.setObjectName("lineEdit_result")self.verticalLayout.addWidget(self.lineEdit_result)self.retranslateUi(Form)QtCore.QMetaObject.connectSlotsByName(Form)def retranslateUi(self, Form):_translate = QtCore.QCoreApplication.translateForm.setWindowTitle(_translate("Form", "密码程序管理"))self.label.setText(_translate("Form", "网站名称:"))self.checkBox_upper.setText(_translate("Form", "大写字母"))self.checkBox_lower.setText(_translate("Form", "小写字母"))self.checkBox_number.setText(_translate("Form", "数字"))self.checkBox_4.setText(_translate("Form", "标点符号"))self.pushButton.setText(_translate("Form", "生成新的密码"))
前端界面设计完成
后端代码实现:
2.代码实现:
"""
1.给不同网站设置密码
2.密码组成方式由 大写,小写,数字,标点符号类别组成
3.生成txt文件保存
"""
#导入模块顺序,系统模块,第三方模块,自己写的模块
import random
import string
import sysfrom PyQt5.QtWidgets import QApplication, QDialog, QMessageBox#导入生成的gui界面的py代码
from passwd import Ui_Formclass MyPassword(Ui_Form,QDialog):def __init__(self):super().__init__()self.setupUi(self)#单击按钮连接一个生成I密码的函数new_passwordself.pushButton.clicked.connect(self.new_password)def new_password(self):#获取网站编辑框的内容site=self.lineEdit_site.text()#判断网站编辑框没有数据,提示对话框显示内容if not site:QMessageBox.warning(self,"信息提示","请输入site")returnwordslist=[]if self.checkBox_upper.isChecked():wordslist.append(string.ascii_uppercase*2)if self.checkBox_lower.isChecked():wordslist.append(string.ascii_lowercase*2)if self.checkBox_number.isChecked():wordslist.append(string.digits*2)#如果字符选择框被选中if self.checkBox_4.isChecked():wordslist.append(string.punctuation*2)#判断words列表是否为空,如果为空说明什么都是没有√选#就是默认使用所有类比生成密码if not wordslist:wordstr=(string.digits+string.ascii_uppercase+string.ascii_lowercase+string.punctuation)else:wordstr="".join(wordslist)#从字符串中选择出20个单个字符的列表【“1”,“2”,“a”。。。】wordstemplist=random.sample(list(wordstr),20)#合并成一个字符串,个数是20个字符的字符串password="".join(wordstemplist)#把生成的密码写入到本地的密码本中。with open("./密码本.txt",mode="a") as fw:fw.write(f"{site}\t{password}\n")self.lineEdit_result.setText(password)QMessageBox.information(self,"信息提示","密码生成成功")if __name__ == '__main__':app=QApplication(sys.argv)mywin=MyPassword()mywin.show()sys.exit(app.exec_())
界面展示:
保存到本地的密码本样式