密码生成器是一种工具,用于根据用户设置的参数生成随机密码。这个工具可以生成各种长度和复杂度的密码,可以包含数字、字母、特殊字符等。
密码生成器的目的是为了提供一个安全的方式来创建密码,以保护用户的账户和数据安全。通过使用密码生成器,用户可以避免使用容易猜测或破解的密码,从而减少账户被盗用的风险。
本文用python设计一个秘密生产器;
生产后效果;
闲话少絮,开始干活;
第一步,电脑需要安装pyqt6,安装代码pip install pyqt6 。安装pyqt6-tools库,主要是获得qtdesign.exe。pip install pyqt6-tools,·还有一个PyQt6-WebEngine:可以在界面中嵌入浏览器
同样pip install pyqt6-webengine。
·其他工具:pyinstaller、pandas、openpyxl、requests等工具有需要用到了就进行安装;
第二步,到安装pyside6文件夹下,找到Designer;
找到designer.exe之后一定要建一个快捷方式,不然后面使用太麻烦了;
第三步,打开QT-designers.exe
现在widget,然后创建;并开始选择对应对话框,进行拖动到界面上;
拖动的名称在突破右边,需要拖动的在左边,比如找一个label 在左侧输入label 然后拖到中间的创口中;对应的一步一步就完成了。保存文件名为****。ui或者直接为123.ui文件
第四步,将ui文件转成py文件;
打开cmd命令提示符文件,找到你存储123.ui的路径,一般用cd +路径;
然后pyuic6 123.ui -o 123.py 回车,你就可以在当前目录下生产123.py文件;若果报错说明环境或路径不对;或者写的不对;
生成代码如下;
# Form implementation generated from reading ui file 'PwdGenerate.ui' # # Created by: PyQt6 UI code generator 6.4.2 # # WARNING: Any manual changes made to this file will be lost when pyuic6 is # run again. Do not edit this file unless you know what you are doing. from PyQt6 import QtCore, QtGui, QtWidgets class Ui_PwdGenerate(object): def setupUi(self, PwdGenerate): PwdGenerate.setObjectName("PwdGenerate") PwdGenerate.resize(693, 240) self.widget = QtWidgets.QWidget(parent=PwdGenerate) self.widget.setGeometry(QtCore.QRect(0, 30, 653, 194)) 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(parent=self.widget) font = QtGui.QFont() font.setPointSize(22) self.label.setFont(font) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEdit_site = QtWidgets.QLineEdit(parent=self.widget) font = QtGui.QFont() font.setPointSize(24) font.setBold(True) self.lineEdit_site.setFont(font) 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(parent=self.widget) font = QtGui.QFont() font.setPointSize(14) self.checkBox_upper.setFont(font) self.checkBox_upper.setObjectName("checkBox_upper") self.horizontalLayout_2.addWidget(self.checkBox_upper) self.checkBox_lower = QtWidgets.QCheckBox(parent=self.widget) font = QtGui.QFont() font.setPointSize(14) self.checkBox_lower.setFont(font) self.checkBox_lower.setObjectName("checkBox_lower") self.horizontalLayout_2.addWidget(self.checkBox_lower) self.checkBox_number = QtWidgets.QCheckBox(parent=self.widget) font = QtGui.QFont() font.setPointSize(14) self.checkBox_number.setFont(font) self.checkBox_number.setObjectName("checkBox_number") self.horizontalLayout_2.addWidget(self.checkBox_number) self.checkBox_puc = QtWidgets.QCheckBox(parent=self.widget) font = QtGui.QFont() font.setPointSize(14) self.checkBox_puc.setFont(font) self.checkBox_puc.setObjectName("checkBox_puc") self.horizontalLayout_2.addWidget(self.checkBox_puc) self.verticalLayout.addLayout(self.horizontalLayout_2) self.pushButton = QtWidgets.QPushButton(parent=self.widget) font = QtGui.QFont() font.setPointSize(24) font.setBold(True) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.lineEdit_result = QtWidgets.QLineEdit(parent=self.widget) font = QtGui.QFont() font.setPointSize(24) font.setBold(True) self.lineEdit_result.setFont(font) self.lineEdit_result.setObjectName("lineEdit_result") self.verticalLayout.addWidget(self.lineEdit_result) self.retranslateUi(PwdGenerate) QtCore.QMetaObject.connectSlotsByName(PwdGenerate) def retranslateUi(self, PwdGenerate): _translate = QtCore.QCoreApplication.translate PwdGenerate.setWindowTitle(_translate("PwdGenerate", "密码生成小程序")) self.label.setText(_translate("PwdGenerate", "请输入设置密码的网站")) self.checkBox_upper.setText(_translate("PwdGenerate", "大写字母")) self.checkBox_lower.setText(_translate("PwdGenerate", "小写字母")) self.checkBox_number.setText(_translate("PwdGenerate", "数字")) self.checkBox_puc.setText(_translate("PwdGenerate", "标点符号")) self.pushButton.setText(_translate("PwdGenerate", "密码生成器"))
第五步:编写主程序
引入信号和槽的概念,然后逐个进行签到编写;
import random from PyQt6.QtWidgets import (QApplication,QDialog,QMessageBox) #QApplication #代表入口程序 # QDialog就是对话窗口 import PyQt6.QtCore from PwdGenerate import Ui_PwdGenerate import sys import string #string 是python 的模块 ####引入一个类函数,类函数当中传入另个参数,一个是Ui_PwdGenerate,就是前面pyqt6生产的文件并导出成py的文件,中一个类函数; ####QDialog是Qt中实现对话框的一种方式,它可以提供丰富的交互功能,方便开发者快速构建用户界面 #创建MYpassword***类函数 class MyPasswordGenerate(Ui_PwdGenerate,QDialog): #构造父类函数 def __init__(self): #super().__init__()就是初始化Ui_PasswordGeneate和QDial这两个类,在MypasswordGenerate的位置,如果不初始化 #就没法引用 super().__init__() #继承父类 self.setupUi(self) #调用父类ui_PasswordG****的函数sheupUi self.show() #显示窗口 ############################################################################ #同按钮之间的联系, self.pushButton.clicked.connect( #参数内部写一个函数,并进行引用 self.new_password ) def new_password(self): site = self.lineEdit_site.text() if not site: QMessageBox.warning(self,"信息提示","请输入信息") return words=[] if self.checkBox_upper.isChecked(): #string.ascii_uppercase 代表的是['ABCDEFGHIJKLMNOPQRSTUVWXYZ'] words.append(string.ascii_uppercase) if self.checkBox_lower.isChecked(): words.append(string.ascii_lowercase) if self.checkBox_number.isChecked(): words.append(string.digits*2) if self.checkBox_puc.isChecked(): words.append(string.punctuation) if not words: words = (string.digits + string.ascii_uppercase +string.ascii_lowercase +string.punctuation ) else: words=''.join(words) words = random.sample(list(words),20) password=''.join(words) self.lineEdit_result.setText(password) with open('我的密码本.txt',"a",encoding="utf8") as Ft: Ft.write(f"{site} \t {password}\n") #运行成功给出一个对话框 QMessageBox.information( self,"信息提示",'密码生成成功' ) if __name__=='__main__': app =QApplication(sys.argv) myPasswordGenerate = MyPasswordGenerate() sys.exit(app.exec())
第六步,就是进行封装,将py文件封装成。exe文件;安装pyinstaller
在cmd命令提示符中输入pyinstaller -F 文件名.py