前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个PyQt5示例(密码记忆箱)

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

作者头像
用户6021899
发布2019-08-14 17:42:55
1.5K0
发布2019-08-14 17:42:55
举报
文章被收录于专栏:Python编程 pyqt matplotlib

下面是一个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源程序放在同一目录。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档