一个PyQt5示例(密码记忆箱)

下面是一个PyQt5程序,功能是保存各个账号密码,防止忘掉。

新建、修改、和删除分别对应工具条上的三个按钮。程序现将账号密码等信息显示在表格里(QTableWidget),确认后立即永久保存/更新到本地数据库,或从本地数据库删除。下次打开就会从本地数据库加载到表格。

import sys from PyQt5 import QtWidgets, QtGui import os import sqlite3

class PWKeeper(QtWidgets.QMainWindow):

def __init__(self): super(PWKeeper, self).__init__() self.initToolbar() self.initDB() self.initGrid() #self.current_row = 0 self.setGeometry(300, 300, 750, 400) self.setWindowTitle('PWKeeper') self.setWindowIcon(QtGui.QIcon('icon.png'))

def initToolbar(self): newAction = QtWidgets.QAction(QtGui.QIcon('new.png'), 'New', self) editAction = QtWidgets.QAction(QtGui.QIcon('edit.png'), 'Edit', self) delAction = QtWidgets.QAction(QtGui.QIcon('del.png'), 'Delete', self) newAction.setShortcut('Ctrl+N') editAction.setShortcut('Ctrl+E') delAction.setShortcut('Delete') newAction.triggered.connect(self.newAction_def) editAction.triggered.connect(self.editAction_def) delAction.triggered.connect(self.delAction_def) self.tb_new = self.addToolBar('New') self.tb_edit = self.addToolBar('Edit') self.tb_del = self.addToolBar('Del') self.tb_new.addAction(newAction) self.tb_edit.addAction(editAction) self.tb_del.addAction(delAction)

def initDB(self): if os.path.exists('info.db'): self.conn = sqlite3.connect('info.db') self.conn.isolation_level = None else: self.conn = sqlite3.connect('info.db') self.conn.isolation_level = None self.conn.execute('''CREATE TABLE INFO (ID int PRIMARY KEY NOT NULL, WEBSITE char(255), USERNAME char(255), PASSWORD char(255), URL char(255))''') cur = self.conn.cursor() cur.execute('SELECT * FROM INFO') self.DB_Data = cur.fetchall()#''''''''''''' #print self.DB_Data cur.close() self.current_row = len(self.DB_Data)#''''''''''''''''''''''' #print self.current_row

def initGrid(self): self.grid = QtWidgets.QTableWidget() self.setCentralWidget(self.grid) self.grid.setColumnCount(5) self.grid.setRowCount(20) column_width = [75, 150, 150, 250,100] for column in range(4): self.grid.setColumnWidth(column, column_width[column]) headerlabels = ['Website', 'Username', 'Password', 'Url','Remark'] self.grid.setHorizontalHeaderLabels(headerlabels) self.grid.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) # self.grid.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) # for i in range(self.current_row): for j in range(1,5): new_item = self.DB_Data[i][j] #print new_item new_item = QtWidgets.QTableWidgetItem(new_item) self.grid.setItem(i,j-1, new_item)

def showDialog(self, ws = '', un = '', pw = '', url = ''): edit_dialog = QtWidgets.QDialog(self) group = QtWidgets.QGroupBox('Edit Info', edit_dialog) lbl_website = QtWidgets.QLabel('Website:', group) le_website = QtWidgets.QLineEdit(group) le_website.setText(ws) lbl_username = QtWidgets.QLabel('Username:', group) le_username = QtWidgets.QLineEdit(group) le_username.setText(un) lbl_password = QtWidgets.QLabel('Password:', group) le_password = QtWidgets.QLineEdit(group) le_password.setText(pw) lbl_url = QtWidgets.QLabel('Url:', group) le_url = QtWidgets.QLineEdit(group) le_url.setText(url) ok_button = QtWidgets.QPushButton('OK', edit_dialog) cancel_button = QtWidgets.QPushButton('CANCEL', edit_dialog) ok_button.clicked.connect(edit_dialog.accept) ok_button.setDefault(True) cancel_button.clicked.connect(edit_dialog.reject)

group_layout = QtWidgets.QVBoxLayout() group_item = [lbl_website, le_website, lbl_username, le_username, lbl_password, le_password, lbl_url, le_url] for item in group_item: group_layout.addWidget(item) group.setLayout(group_layout) group.setFixedSize(group.sizeHint())

button_layout = QtWidgets.QHBoxLayout() button_layout.addWidget(ok_button) button_layout.addWidget(cancel_button)

dialog_layout = QtWidgets.QVBoxLayout() dialog_layout.addWidget(group) dialog_layout.addLayout(button_layout) edit_dialog.setLayout(dialog_layout) edit_dialog.setFixedSize(edit_dialog.sizeHint())

if edit_dialog.exec_(): website = le_website.text() username = le_username.text() password = le_password.text() url = le_url.text() return True, website, username, password, url return False, None, None, None, None

def showHint(self): hint_msg = QtWidgets.QMessageBox() hint_msg.setText('No selected row!') hint_msg.addButton(QtWidgets.QMessageBox.Ok) hint_msg.exec_()

def newAction_def(self): data = self.showDialog() if data[0]: self.current_row += 1 self.conn.execute("INSERT INTO INFO VALUES(%d, '%s', '%s', '%s', '%s')" % (self.current_row, data[1], data[2], data[3], data[4])) self.grid.insertRow(self.current_row - 1) for i in range(4): new_item = QtWidgets.QTableWidgetItem(data[i + 1]) self.grid.setItem(self.current_row - 1, i, new_item)

def editAction_def(self): selected_row = self.grid.selectedItems() if selected_row: edit_row = self.grid.row(selected_row[0]) old_data = [] for i in range(4): old_data.append(self.grid.item(edit_row, i).text()) new_data = self.showDialog(*old_data) if new_data[0]: self.conn.execute('''UPDATE INFO SET WEBSITE = '%s', USERNAME = '%s', PASSWORD = '%s', URL = '%s' WHERE ID = '%d' ''' % (new_data[1], new_data[2], new_data[3], new_data[4], edit_row + 1)) for i in range(4): new_item = QtWidgets.QTableWidgetItem(new_data[i + 1]) self.grid.setItem(edit_row, i, new_item) else: self.showHint()

def delAction_def(self): selected_row = self.grid.selectedItems() if selected_row: del_row = self.grid.row(selected_row[0]) self.grid.removeRow(del_row) #print del_row self.conn.execute("DELETE FROM INFO WHERE ID = %d" % (del_row + 1)) for index in range(del_row + 2, self.current_row + 1): self.conn.execute("UPDATE INFO SET ID = %d WHERE ID = %d" % ((index - 1), index)) self.current_row -= 1 else: self.showHint()

if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) pwk = PWKeeper() pwk.show() app.exec_() pwk.conn.close() sys.exit(0)

如果需要图标文件以及源文件,可以私信我。

为了保证本程序中图标的正确显示,请将图标文件和python源程序放在同一目录。

原文发布于微信公众号 - Python编程 pyqt matplotlib(wsplovePython)

原文发表时间:2019-01-19

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券