1. 首页
  2. IT资讯

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

“u003Cdivu003Eu003Cp class=”ql-align-justify”u003E下面是一个PyQt5程序,功能是保存各个账号密码,防止忘掉。u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E程序先将账号密码等信息显示在表格里(QTableWidget),确认后立即永久保存u002F更新到本地数据库,或从本地数据库删除。下次打开就会从本地数据库加载到表格。u003Cu002Fpu003Eu003Cp class=”ql-align-center”u003Eu003Cbru003Eu003Cu002Fpu003Eu003Cdiv class=”pgc-img”u003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002F17a5a5e7e02b4ae1afed67d68a1eff8d” img_width=”806″ img_height=”350″ alt=”一个PyQt5示例(密码记忆箱)” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cp class=”ql-align-center”u003Eu003Cbru003Eu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Eimport sysu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Efrom PyQt5 import QtWidgets, QtGuiu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Eimport osu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Eimport sqlite3u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Eclass PWKeeper(QtWidgets.QMainWindow):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def __init__(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E super(PWKeeper, self).__init__()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.initToolbar()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.initDB()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.initGrid()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E #self.current_row = 0u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.setGeometry(300, 300, 750, 400)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.setWindowTitle(‘PWKeeper’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.setWindowIcon(QtGui.QIcon(‘icon.png’))u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def initToolbar(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E newAction = QtWidgets.QAction(QtGui.QIcon(‘new.png’), ‘New’, self)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E editAction = QtWidgets.QAction(QtGui.QIcon(‘edit.png’), ‘Edit’, self)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E delAction = QtWidgets.QAction(QtGui.QIcon(‘del.png’), ‘Delete’, self)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E newAction.setShortcut(‘Ctrl+N’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E editAction.setShortcut(‘Ctrl+E’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E delAction.setShortcut(‘Delete’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E newAction.triggered.connect(self.newAction_def)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E editAction.triggered.connect(self.editAction_def)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E delAction.triggered.connect(self.delAction_def)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_new = self.addToolBar(‘New’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_edit = self.addToolBar(‘Edit’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_del = self.addToolBar(‘Del’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_new.addAction(newAction)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_edit.addAction(editAction)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.tb_del.addAction(delAction)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def initDB(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if os.path.exists(‘info.db’):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn = sqlite3.connect(‘info.db’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.isolation_level = Noneu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E else:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn = sqlite3.connect(‘info.db’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.isolation_level = Noneu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.execute(”’CREATE TABLE INFOu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E (ID int PRIMARY KEY NOT NULL,u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E WEBSITE char(255),u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E USERNAME char(255),u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E PASSWORD char(255),u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E URL char(255))”’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E cur = self.conn.cursor()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E cur.execute(‘SELECT * FROM INFO’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.DB_Data = cur.fetchall()#””””””’u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E #print self.DB_Datau003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E cur.close()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.current_row = len(self.DB_Data)#”””””””””””’u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E #print self.current_rowu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def initGrid(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid = QtWidgets.QTableWidget()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.setCentralWidget(self.grid)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setColumnCount(5)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setRowCount(20)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E column_width = [75, 150, 150, 250,100]u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for column in range(4):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setColumnWidth(column, column_width[column])u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E headerlabels = [‘Website’, ‘Username’, ‘Password’, ‘Url’,’Remark’]u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setHorizontalHeaderLabels(headerlabels)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) #u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) #u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for i in range(self.current_row):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for j in range(1,5):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E new_item = self.DB_Data[i][j]u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E #print new_itemu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E new_item = QtWidgets.QTableWidgetItem(new_item)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setItem(i,j-1, new_item)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def showDialog(self, ws = ”, un = ”, pw = ”, url = ”):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E edit_dialog = QtWidgets.QDialog(self)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group = QtWidgets.QGroupBox(‘Edit Info’, edit_dialog)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_website = QtWidgets.QLabel(‘Website:’, group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_website = QtWidgets.QLineEdit(group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_website.setText(ws)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_username = QtWidgets.QLabel(‘Username:’, group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_username = QtWidgets.QLineEdit(group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_username.setText(un)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_password = QtWidgets.QLabel(‘Password:’, group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_password = QtWidgets.QLineEdit(group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_password.setText(pw)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_url = QtWidgets.QLabel(‘Url:’, group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_url = QtWidgets.QLineEdit(group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E le_url.setText(url)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E ok_button = QtWidgets.QPushButton(‘OK’, edit_dialog)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E cancel_button = QtWidgets.QPushButton(‘CANCEL’, edit_dialog)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E ok_button.clicked.connect(edit_dialog.accept)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E ok_button.setDefault(True)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E cancel_button.clicked.connect(edit_dialog.reject)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group_layout = QtWidgets.QVBoxLayout()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group_item = [lbl_website, le_website,u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_username, le_username,u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_password, le_password,u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E lbl_url, le_url]u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for item in group_item:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group_layout.addWidget(item)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group.setLayout(group_layout)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E group.setFixedSize(group.sizeHint())u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E button_layout = QtWidgets.QHBoxLayout()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E button_layout.addWidget(ok_button)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E button_layout.addWidget(cancel_button)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E dialog_layout = QtWidgets.QVBoxLayout()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E dialog_layout.addWidget(group)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E dialog_layout.addLayout(button_layout)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E edit_dialog.setLayout(dialog_layout)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E edit_dialog.setFixedSize(edit_dialog.sizeHint())u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if edit_dialog.exec_():u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E website = le_website.text()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E username = le_username.text()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E password = le_password.text()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E url = le_url.text()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E return True, website, username, password, urlu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E return False, None, None, None, Noneu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def showHint(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E hint_msg = QtWidgets.QMessageBox()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E hint_msg.setText(‘No selected row!’)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E hint_msg.addButton(QtWidgets.QMessageBox.Ok)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E hint_msg.exec_()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def newAction_def(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E data = self.showDialog()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if data[0]:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.current_row += 1u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.execute(“INSERT INTO INFO VALUES(%d, ‘%s’, ‘%s’, ‘%s’, ‘%s’)”u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E % (self.current_row, data[1], data[2], data[3], data[4]))u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.insertRow(self.current_row – 1)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for i in range(4):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E new_item = QtWidgets.QTableWidgetItem(data[i + 1])u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setItem(self.current_row – 1, i, new_item)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def editAction_def(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E selected_row = self.grid.selectedItems()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if selected_row:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E edit_row = self.grid.row(selected_row[0])u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E old_data = []u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for i in range(4):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E old_data.append(self.grid.item(edit_row, i).text())u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E new_data = self.showDialog(*old_data)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if new_data[0]:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.execute(”’UPDATE INFO SETu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E WEBSITE = ‘%s’, USERNAME = ‘%s’,u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E PASSWORD = ‘%s’, URL = ‘%s’u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E WHERE ID = ‘%d’ ”’u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E % (new_data[1], new_data[2], new_data[3], new_data[4], edit_row + 1))u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for i in range(4):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E new_item = QtWidgets.QTableWidgetItem(new_data[i + 1])u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.setItem(edit_row, i, new_item)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E else:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.showHint()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E def delAction_def(self):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E selected_row = self.grid.selectedItems()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E if selected_row:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E del_row = self.grid.row(selected_row[0])u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.grid.removeRow(del_row)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E #print del_rowu003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.execute(“DELETE FROM INFO WHERE ID = %d” % (del_row + 1))u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E for index in range(del_row + 2, self.current_row + 1):u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.conn.execute(“UPDATE INFO SET ID = %d WHERE ID = %d” % ((index – 1), index))u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.current_row -= 1u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E else:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E self.showHint()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003Eif __name__ == ‘__main__’:u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E app = QtWidgets.QApplication(sys.argv)u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E pwk = PWKeeper()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E pwk.show()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E app.exec_()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E pwk.conn.close()u003Cu002Fpu003Eu003Cp class=”ql-align-justify”u003E sys.exit(0)u003Cu002Fpu003Eu003Cu002Fdivu003E”

原文始发于:一个PyQt5示例(密码记忆箱)

主题测试文章,只做测试使用。发布者:玩家L-,转转请注明出处:http://www.cxybcw.com/10686.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code