密码管理系统(pyqt+sqllite)
前言: 由于现在密码越来越多,老是容易忘记,抽空就开发了一个密码管理系统
主要技能: python,qt,sqllite
技术亮点:
1.pyqt 信号与槽,页面间传值
2.tableWidget 右击菜单,双击事件
3.界面图片打包进程序
4.界面伸缩布局
5.sql数据库语法(模糊查询)
6.页面跳转
7.QtWidget生命周期事件
主程序登录界面:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'wb.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
import sign_in
import sys
import connectData
import password_screen
import background_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.MainWindow = MainWindow
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/wb.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.SQL = connectData.sql()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setObjectName("widget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_3.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_4.addWidget(self.pushButton, 0, QtCore.Qt.AlignHCenter)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_4.addWidget(self.pushButton_2, 0, QtCore.Qt.AlignHCenter)
self.verticalLayout.addLayout(self.horizontalLayout_4)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.horizontalLayout.addWidget(self.widget)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem3)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.show_sign_in_windows)
self.pushButton_2.clicked.connect(self.show_logon_windows)
self.lineEdit_2.returnPressed.connect(self.show_logon_windows)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def show_sign_in_windows(self):
self.Dialog = QtWidgets.QDialog()
self.ui2 = sign_in.Ui_Dialog()
self.ui2.closeWindow.connect(self.MainWindow_show)
self.ui2.setupUi(self.Dialog)
self.Dialog.show()
MainWindow.hide()
def show_logon_windows(self):
if self.SQL.chack_user(self.lineEdit.text(),self.lineEdit_2.text()):
# self.Dialog = QtWidgets.QDialog()
self.ui2 = password_screen.Ui_Dialog(self.lineEdit.text())
self.ui2.closeWindow.connect(self.MainWindow_show)
# self.ui2.setupUi(self.Dialog)
# self.Dialog.show()
self.ui2.show()
MainWindow.hide()
else:
QtWidgets.QMessageBox.about(self.MainWindow,"提示","用户名或密码不正确!")
def MainWindow_show(self,val):
self.lineEdit.setText(val)
self.lineEdit_2.setText('')
MainWindow.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "个人密码管理系统"))
self.label.setText(_translate("MainWindow", "用户名:"))
self.label_2.setText(_translate("MainWindow", "密 码:"))
self.pushButton.setText(_translate("MainWindow", "注册"))
self.pushButton_2.setText(_translate("MainWindow", "登录"))
if __name__ =='__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
操作数据
import sqlite3
class sql():
def __init__(self):
self.conn = sqlite3.connect('data.db')
c = self.conn.cursor()
try:
create_tb_cmd='''
CREATE TABLE IF NOT EXISTS USER
( NAME char(50) NOT NULL,\
PASSWORD char(50) NOT NULL,\
LEVEL integer DEFAULT 1,\
primary key (NAME));
'''
c.execute(create_tb_cmd)
create_password_cmd = '''
CREATE TABLE IF NOT EXISTS PASSWORD
(ID INTEGER PRIMARY KEY autoincrement,\
application char(50) NOT NULL,\
NAME char(50) NOT NULL,\
PASSWORD char(50) NOT NULL,\
user char(50) NOT NULL,\
CreatedTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime'))
);
'''
c.execute(create_password_cmd)
self.conn.commit()
# self.conn.close()
except Exception as e:
print(e)
print ("Create table failed")
def chack_user(self,user,password):
try:
c = self.conn.cursor()
chack_pass_cmd = 'select PASSWORD from user where NAME == "{}"'.format(user)
c.execute(chack_pass_cmd)
self.conn.commit()
passwd_list = c.fetchall()
print(passwd_list)
if passwd_list != []:
if passwd_list[0][0] == password:
return True
else:
return False
return False
except Exception as e:
print(e)
return False
def insert_user(self,name,password):
try:
c = self.conn.cursor()
insert_user_cmd = 'insert into user (NAME,PASSWORD) values ("{}","{}")'.format(name,password)
c.execute(insert_user_cmd)
self.conn.commit()
return True
except Exception as e:
print(e)
return False
def insert_passwd(self,application,name,passwd,user):
try:
c = self.conn.cursor()
insert_passwd_cmd = 'insert into PASSWORD (application,NAME,PASSWORD,user) values ("{}","{}","{}","{}")'.format(application,name,passwd,user)
c.execute(insert_passwd_cmd)
self.conn.commit()
return True
except Exception as e:
print(e)
return False
def select_passwdByName(self,application):
try:
c = self.conn.cursor()
insert_passwd_cmd = 'select * from PASSWORD where application = "{}" order by CreatedTime desc'.format(application)
c.execute(insert_passwd_cmd)
self.conn.commit()
passwd_list = c.fetchall()
print(passwd_list)
return passwd_list
except Exception as e:
print(e)
return False
def delete_passwd(self,application,time,user):
try:
c = self.conn.cursor()
delete_passwd_cmd = 'delete from PASSWORD where application="{}" and CreatedTime= "{}" and user = "{}"'.format(application,time,user)
c.execute(delete_passwd_cmd)
self.conn.commit()
return True
except Exception as e:
print(e)
return False
def update_passwd(self,password,application,time,user):
try:
c = self.conn.cursor()
update_passwd_cmd = 'update PASSWORD set PASSWORD = "{}" where application="{}" and CreatedTime= "{}" and user = "{}"'.format(password,application,time,user)
c.execute(update_passwd_cmd)
self.conn.commit()
return True
except Exception as e:
print(e)
return False
def select_passwd_all(self,user):
try:
c = self.conn.cursor()
insert_passwd_cmd = 'select * from PASSWORD where user = "{}" order by application asc,CreatedTime DESC'.format(user)
c.execute(insert_passwd_cmd)
self.conn.commit()
passwd_list = c.fetchall()
# print(passwd_list)
return passwd_list
except Exception as e:
print(e)
return []
def select_passwd_by_like(self,str,user):
try:
c = self.conn.cursor()
insert_passwd_cmd = 'select * from PASSWORD where user = "{}" and application like "%{}%" order by application asc,CreatedTime DESC'.format(user,str)
c.execute(insert_passwd_cmd)
self.conn.commit()
passwd_list = c.fetchall()
# print(passwd_list)
return passwd_list
except Exception as e:
print(e)
return []
def close(self):
try:
self.conn.close()
except:
pass
显示密码界面
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'password_screen.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *
import connectData
import password
import search
import background_rc
class Ui_Dialog(QDialog):
closeWindow = pyqtSignal(str)
def __init__(self,user):
super(QDialog, self).__init__()
self.user = user
self.search_str = ''
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(750, 600)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/wb.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.setWindowFlags(QtCore.Qt.WindowMaximizeButtonHint | QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.groupBox = QtWidgets.QGroupBox(Dialog)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_5.setMinimumSize(QtCore.QSize(20, 20))
self.pushButton_5.setMaximumSize(QtCore.QSize(20, 20))
self.pushButton_5.setStyleSheet("\n"
"QPushButton{background-color:rgb(104, 104, 104);color: white; border: 2px groove gray;border-style: outset;\n"
"image: url(:/image/select.ico);}\n"
"\n"
"QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset;border-image: url(:/image/select.ico); }")
self.pushButton_5.setText("")
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout.addWidget(self.pushButton_5)
self.pushButton = QtWidgets.QPushButton(self.groupBox)
self.pushButton.setMinimumSize(QtCore.QSize(20, 20))
self.pushButton.setMaximumSize(QtCore.QSize(20, 20))
self.pushButton.setStyleSheet("\n"
"QPushButton{background-color:rgb(104, 104, 104);color: white; border: 2px groove gray;border-style: outset;\n"
"image: url(:/image/add.ico);}\n"
"\n"
"QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset;border-image: url(:/image/add.ico); }")
self.pushButton.setText("")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setMinimumSize(QtCore.QSize(20, 20))
self.pushButton_2.setMaximumSize(QtCore.QSize(20, 20))
self.pushButton_2.setStyleSheet("\n"
"QPushButton{background-color:rgb(104, 104, 104);color: white; border: 2px groove gray;border-style: outset;\n"
"image: url(:/image/delete.ico);}\n"
"\n"
"QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset;border-image: url(:/image/delete.ico); }")
self.pushButton_2.setText("")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.tableWidget = QtWidgets.QTableWidget(self.groupBox)
self.tableWidget.setEnabled(True)
self.tableWidget.setToolTip("")
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3.addLayout(self.verticalLayout)
self.verticalLayout_2.addWidget(self.groupBox)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_5.setToolTip(_translate("Dialog", "查找"))
self.pushButton.setToolTip(_translate("Dialog", "添加"))
self.pushButton_2.setToolTip(_translate("Dialog", "删除"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Dialog", "应用"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("Dialog", "用户名"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("Dialog", "密码"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("Dialog", "时间"))
self.pushButton.clicked.connect(self.addclick)
self.pushButton_2.clicked.connect(self.deleteclick)
self.pushButton_5.clicked.connect(self.search_app)
Dialog.setWindowTitle(_translate("Dialog", "{}".format(self.user)))
self.SQL = connectData.sql()
self.tableWidget.setColumnWidth(3, 200)
self.update_UI()
self.tableWidget.doubleClicked.connect(self.table_change)
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) ######允许右键产生子菜单
self.tableWidget.customContextMenuRequested.connect(self.custom_right_menu) ####右键菜单
def custom_right_menu(self, pos):
try:
menu = QMenu()
opt2 = menu.addAction("添加")
opt1 = menu.addAction("删除")
action = menu.exec_(self.tableWidget.mapToGlobal(pos))
if action == opt1:
self.deleteclick()
elif action == opt2:
application = self.tableWidget.item(self.tableWidget.currentRow(), 0).text()
name = self.tableWidget.item(self.tableWidget.currentRow(), 1).text()
# self.Dialog = QtWidgets.QDialog()
self.ui2 = password.Ui_Dialog(self.user, application, name, '', '', False)
self.ui2.closeWindow.connect(self.update_UI)
# self.ui2.setupUi(self.Dialog)
self.ui2.show()
else:
return
except Exception as e:
print(e)
def table_change(self):
try:
application = self.tableWidget.item(self.tableWidget.currentRow(), 0).text()
name = self.tableWidget.item(self.tableWidget.currentRow(), 1).text()
password_str = self.tableWidget.item(self.tableWidget.currentRow(), 2).text()
time_str = self.tableWidget.item(self.tableWidget.currentRow(), 3).text()
# self.Dialog = QtWidgets.QDialog()
self.ui2 = password.Ui_Dialog(self.user, application, name, password_str, time_str, True)
self.ui2.closeWindow.connect(self.update_UI)
# self.ui2.setupUi(self.Dialog)
self.ui2.show()
except Exception as e:
print(e)
def update_UI(self, app_str=''):
self.search_str = app_str
if self.search_str == '':
password_list = self.SQL.select_passwd_all(self.user)
_translate = QtCore.QCoreApplication.translate
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Dialog", "应用"))
else:
password_list = self.SQL.select_passwd_by_like(app_str, self.user)
_translate = QtCore.QCoreApplication.translate
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("Dialog", "应用({})".format(self.search_str)))
print(password_list)
self.tableWidget.setRowCount(len(password_list))
for index1, data in enumerate(password_list):
self.tableWidget.setItem(index1, 0, QTableWidgetItem(data[1]))
self.tableWidget.setItem(index1, 1, QTableWidgetItem(data[2]))
self.tableWidget.setItem(index1, 2, QTableWidgetItem(data[3]))
self.tableWidget.setItem(index1, 3, QTableWidgetItem(data[5]))
def addclick(self):
# self.Dialog = QtWidgets.QDialog()
self.ui2 = password.Ui_Dialog(self.user)
self.ui2.closeWindow.connect(self.update_UI)
# self.ui2.setupUi(self.Dialog)
self.ui2.show()
def deleteclick(self):
reply = QtWidgets.QMessageBox.question(self,
'确认',
"是否要删除这条记录?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
try:
application = self.tableWidget.item(self.tableWidget.currentRow(), 0).text()
name = self.tableWidget.item(self.tableWidget.currentRow(), 1).text()
password = self.tableWidget.item(self.tableWidget.currentRow(), 2).text()
time_str = self.tableWidget.item(self.tableWidget.currentRow(), 3).text()
if self.SQL.delete_passwd(application, time_str, self.user):
self.tableWidget.removeRow(self.tableWidget.currentRow())
else:
QtWidgets.QMessageBox.about(self, '提示', '删除失败!')
except:
QtWidgets.QMessageBox.about(self, '提示', '请选择删除项!')
else:
pass
def search_app(self):
self.ui2 = search.Ui_Dialog(self.search_str)
self.ui2.closeWindow.connect(self.update_UI)
# self.ui2.setupUi(self.Dialog)
self.ui2.show()
def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
self.closeWindow.emit(self.user)
注册用户界面
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'sign_in.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *
import connectData
import background_rc
class Ui_Dialog(QDialog):
closeWindow = pyqtSignal(str)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/wb.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
Dialog.setWindowFlags(
QtCore.Qt.WindowMaximizeButtonHint | QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)
self.Dialog = Dialog
self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(Dialog)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_3.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setObjectName("label_3")
self.horizontalLayout_5.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_5.addWidget(self.lineEdit_3)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem2)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_4.addWidget(self.pushButton, 0, QtCore.Qt.AlignHCenter)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem3)
self.verticalLayout.addLayout(self.horizontalLayout_4)
spacerItem4 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem4)
self.horizontalLayout.addLayout(self.verticalLayout)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem5)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "注册"))
self.label.setText(_translate("Dialog", "用户名:"))
self.label_2.setText(_translate("Dialog", "密 码:"))
self.label_3.setText(_translate("Dialog", "确 认:"))
self.pushButton.setText(_translate("Dialog", "注册"))
self.pushButton.clicked.connect(self.sign_in)
def sign_in(self):
self.SQL = connectData.sql()
if self.lineEdit_2.text() == self.lineEdit_3.text():
if self.SQL.insert_user(self.lineEdit.text(),self.lineEdit_2.text()):
self.closeWindow.emit(str(self.lineEdit.text()))
self.SQL.close()
self.Dialog.close()
else:
QtWidgets.QMessageBox.about(self.Dialog,'提示','用户已存在!')
else:
QtWidgets.QMessageBox.about(self.Dialog,'提示','请确认密码!')
添加修改密码界面
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'password.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *
import connectData
import background_rc
class Ui_Dialog(QDialog):
closeWindow = pyqtSignal()
def __init__(self, user,application = '',name = '',password = '',time_str = '',update = False):
super(QDialog, self).__init__()
self.user = user
self.application = application
self.name = name
self.password = password
self.update_tag = update
self.time_str = time_str
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(320, 220)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/wb.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.setWindowFlags(
QtCore.Qt.WindowMaximizeButtonHint | QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)
self.Dialog = Dialog
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Dialog)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
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(Dialog)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_3.addWidget(self.lineEdit_3)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton, 0, QtCore.Qt.AlignHCenter)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
self.horizontalLayout_4.addLayout(self.verticalLayout)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem3)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "密码"))
self.label.setText(_translate("Dialog", "应用名称:"))
self.label_2.setText(_translate("Dialog", "用 户 名:"))
self.label_3.setText(_translate("Dialog", "密 码:"))
self.pushButton.setText(_translate("Dialog", "确定"))
if self.update_tag:
self.lineEdit.setDisabled(True)
self.lineEdit_2.setDisabled(True)
self.lineEdit.setText(self.application)
self.lineEdit_2.setText(self.name)
self.lineEdit_3.setText(self.password)
self.pushButton.clicked.connect(self.add_password)
self.SQL = connectData.sql()
def add_password(self):
if self.update_tag:
if self.SQL.update_passwd(self.lineEdit_3.text(),self.lineEdit.text(),self.time_str,self.user):
self.SQL.close()
self.closeWindow.emit()
self.Dialog.close()
else:
QtWidgets.QMessageBox.about(self, '提示', '错误!')
# self.Dialog.close()
else:
if self.SQL.insert_passwd(self.lineEdit.text(),self.lineEdit_2.text(),self.lineEdit_3.text(),self.user):
self.SQL.close()
self.closeWindow.emit()
self.Dialog.close()
else:
QtWidgets.QMessageBox.about(self, '提示', '错误!')
查询界面
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'search.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import *
import background_rc
class Ui_Dialog(QDialog):
closeWindow = pyqtSignal(str)
def __init__(self,search_str):
super(QDialog, self).__init__()
self.search_str = search_str
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(300, 150)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icon/wb.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
self.setWindowFlags(
QtCore.Qt.WindowMaximizeButtonHint | QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(Dialog)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.verticalLayout.addLayout(self.horizontalLayout)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_2.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem3)
self.horizontalLayout_3.addLayout(self.verticalLayout)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem4)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "查找"))
self.label.setText(_translate("Dialog", "查找:"))
self.pushButton.setText(_translate("Dialog", "确定"))
self.pushButton_2.setText(_translate("Dialog", "取消"))
self.lineEdit.setText(self.search_str)
self.pushButton.clicked.connect(self.search_app)
self.pushButton_2.clicked.connect(self.cancel_search)
def search_app(self):
self.closeWindow.emit(self.lineEdit.text())
self.close()
def cancel_search(self):
self.closeWindow.emit('')
self.close()
这样就完成了本机的密码管理系统,代码还有很大的优化空间。