python 本地密码管理应用

# coding:utf-8
import sys
import sqlite3
from PyQt5.QtWidgets import QApplication, QTableWidget, QLineEdit, QPushButton, QTableWidgetItem, QWidget, QLabel, QVBoxLayout, QMessageBox, QDialog

class Login(QDialog):
def __init__(self):
super().__init__()
self.initLogin()

def initLogin(self):
layout = QVBoxLayout()
self.username = QLineEdit()
self.username.setPlaceholderText(“username”)
self.password = QLineEdit()
self.password.setPlaceholderText(“password”)
self.password.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton(‘Login’)
self.login_button.clicked.connect(self.login)
layout.addWidget(QLabel(“用户名:”))
layout.addWidget(self.username)
layout.addWidget(QLabel(“密码:”))
layout.addWidget(self.password)
layout.addWidget(self.login_button)
self.setLayout(layout)
self.setGeometry(300, 300, 350, 200)
self.setWindowTitle(‘Login’)
self.show()

def login(self):
username = self.username.text()
password = self.password.text()
conn = sqlite3.connect(‘cryptogram.db’)
cursor = conn.cursor()
cursor.execute(“CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, password TEXT NOT NULL, is_login INTEGER)”)
cursor.execute(“SELECT * FROM user WHERE name=?”, (username,))
row = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
if row:
if username == row[1] and password == row[2]:
self.accept()
return username
else:
QMessageBox.warning(self, ‘Login Failed’, ‘Incorrect username or password’)
return None
else:
QMessageBox.warning(self, ‘Login Failed’, ‘User does not exist’)
self.close()
return None

class Register(QWidget):
def __init__(self):
super().__init__()
self.init_register()

def init_register(self):
layout = QVBoxLayout()
self.username = QLineEdit()
self.username.setPlaceholderText(“name”)
self.password = QLineEdit()
self.password.setPlaceholderText(“password”)
self.password.setEchoMode(QLineEdit.Password)
self.reg_button = QPushButton(‘Register’)
self.reg_button.clicked.connect(self.reg)
layout.addWidget(QLabel(“用户名:”))
layout.addWidget(self.username)
layout.addWidget(QLabel(“密码:”))
layout.addWidget(self.password)
layout.addWidget(self.reg_button)
self.setLayout(layout)
self.setGeometry(300, 300, 350, 200)
self.setWindowTitle(‘Register’)
self.show()

def reg(self):
username = self.username.text()
password = self.password.text()
if not username or not password:
QMessageBox.warning(self, ‘Registration Failed’, ‘Username and password cannot be empty’)
conn = sqlite3.connect(‘cryptogram.db’)
cursor = conn.cursor()
cursor.execute(“SELECT * FROM user WHERE name=?”, (username,))
if cursor.fetchone():
QMessageBox.warning(self, ‘Registration Failed’, ‘User already exists’)
else:
cursor.execute(“INSERT INTO user (name, password) VALUES (?, ?)”, (username, password))
conn.commit()
QMessageBox.information(self, ‘Registration Successful’, ‘User registered successfully’)
self.close()
cursor.close()
conn.close()

class TableList(QWidget):
def __init__(self, admin):
super().__init__()
self.admin = admin
self.initUI()

def initUI(self):
self.tableWidget = QTableWidget(self)
self.tableWidget.setColumnCount(3)
self.tableWidget.setHorizontalHeaderLabels([‘Website’, ‘Username’, ‘Password’])
self.load_data()
layout = QVBoxLayout()
layout.addWidget(self.tableWidget)
self.setLayout(layout)
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle(‘Password Manager’)
self.show()

def load_data(self):
conn = sqlite3.connect(‘cryptogram.db’)
cursor = conn.cursor()
cursor.execute(“SELECT website, username, password FROM cryptogram WHERE admin=?”, (self.admin,))
rows = cursor.fetchall()
self.tableWidget.setRowCount(len(rows))
for i, row in enumerate(rows):
for j, data in enumerate(row):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(data)))
conn.commit()
cursor.close()
conn.close()

class Password(QWidget):
def __init__(self, admin):
super().__init__()
self.admin = admin
self.initUI()

def initUI(self):
self.website = QLineEdit()
self.username = QLineEdit()
self.password = QLineEdit()
self.password.setEchoMode(QLineEdit.Password)
self.add_button = QPushButton(‘Add Password’)
self.add_button.clicked.connect(self.save_password)
layout = QVBoxLayout()
layout.addWidget(QLabel(“Website:”))
layout.addWidget(self.website)
layout.addWidget(QLabel(“Username:”))
layout.addWidget(self.username)
layout.addWidget(QLabel(“Password:”))
layout.addWidget(self.password)
layout.addWidget(self.add_button)
self.setLayout(layout)
self.setGeometry(300, 300, 350, 200)
self.setWindowTitle(‘Add Password’)
self.show()

def save_password(self):
website = self.website.text()
username = self.username.text()
password = self.password.text()
conn = sqlite3.connect(‘cryptogram.db’)
cursor = conn.cursor()
cursor.execute(“INSERT INTO cryptogram (website, username, password, admin) VALUES (?, ?, ?, ?)”, (website, username, password, self.admin))
conn.commit()
cursor.close()
conn.close()
QMessageBox.information(self, ‘Password Added’, ‘Password added successfully’)
self.close()

def init_database():
conn = sqlite3.connect(‘cryptogram.db’)
cursor = conn.cursor()
cursor.execute(“CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, password TEXT NOT NULL, is_login INTEGER)”)
cursor.execute(“CREATE TABLE IF NOT EXISTS cryptogram (id INTEGER PRIMARY KEY AUTOINCREMENT, website TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, admin TEXT NOT NULL)”)
conn.commit()
cursor.close()
conn.close()

def main():
init_database()
app = QApplication(sys.argv)
login_dialog = Login()
if login_dialog.exec_() == QDialog.Accepted:
admin = login_dialog.username.text()
table_list = TableList(admin)
password_dialog = Password(admin)
else:
login_dialog.close()
register_dialog = Register()
register_dialog.show()
sys.exit(app.exec_())

if __name__ == ‘__main__’:
main()

发表评论