PyQt4百行代码自制密码管理器(一):基本框架搭建
2016-02-27 14:39
375 查看
1.引言
很多时候,教程只是教给你了模块使用上的基础知识,而各个模块就像积木一样,只有把他们合理地拼在一起,才能构建成一个完整、有价值的应用。这里,我们就来进行一次PyQt4的实例开发,我将简单介绍如何用短短百行代码实现一个密码管理器。首先,你的电脑应该具备Python+PyQt的开发环境,我这里使用的版本是Python2.7+PyQt4。其次,你应该具备基本的Python语法知识、一些面向对象的编程思想和一些PyQt方面的知识。
作者能力有限,还请大家包涵,欢迎提出建议!
那么,我们开始吧!
2.基本框架设计
我们的简单密码管理器应该具备如下最基本的功能:- 可以以表格形式显示出已有的账户信息;
- 可以新建账户信息;
- 可以编辑修改已有的账户信息;
- 可以删除已有的账户信息;
- 关闭程序后,信息不会丢失;
接着,我们想像应用的图形界面应该是这个样子的【要求不要太高嘛】:
这里我们就应该对这个程序怎么写有一个大致的想法了:最上面一个toolbar,中间是一个table,数据存储在数据库中。
那么下面就是具体的实现了。
3.写出基本框架
我们新建一个Python工程,叫做PWKeeper(密码管家)。这里顺便安利一下PyCharm这款IDE,用着很顺手。官网:
http://www.jetbrains.com/pycharm/download/
我们写出一些脚手架的代码:
import sys from PyQt4 import QtGui class PWKeeper(QtGui.QMainWindow): def __init__(self): super(PWKeeper, self).__init__() self.initToolbar() self.initDB() self.initGrid() self.current_row = 0 self.setGeometry(300, 300, 650, 300) self.setWindowTitle('PWKeeper') self.setWindowIcon(QtGui.QIcon('icon.png')) def initToolbar(self): newAction = QtGui.QAction(QtGui.QIcon('new.png'), 'New', self) editAction = QtGui.QAction(QtGui.QIcon('edit.png'), 'New', self) delAction = QtGui.QAction(QtGui.QIcon('del.png'), 'New', 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): pass def initGrid(self): self.grid = QtGui.QTableWidget() self.setCentralWidget(self.grid) self.grid.setColumnCount(4) self.grid.setRowCount(0) column_width = [75, 150, 270, 150] for column in range(4): self.grid.setColumnWidth(column, column_width[column]) headerlabels = ['Website', 'Username', 'Password', 'Url'] self.grid.setHorizontalHeaderLabels(headerlabels) self.grid.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.grid.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) def newAction_def(self): pass def editAction_def(self): pass def delAction_def(self): pass if __name__ == '__main__': app = QtGui.QApplication(sys.argv) pwk = PWKeeper() pwk.show() sys.exit(app.exec_())
大部分都非常浅显易懂。我们用initToolbar()创建工具条,用initDB()初始化数据库(还没有实现),用initGrid()创建表格,而current_row这个变量,我们下一节会用到。
代码中使用的一些图标文件在本节末尾会附上。
newAction.triggered.connect(self.newAction_def) editAction.triggered.connect(self.editAction_def) delAction.triggered.connect(self.delAction_def)
这三句我们将三种Action和我们自己定义的函数相关联(但还没有实现)。
主要讲一下关于表格的内容。创建表格时我们使用的是QtGui.QTableWidget类。
self.grid.setColumnCount(4) self.grid.setRowCount(0)
这两句是设定初始的行数、列数。我们有四列,分别存放网站名、用户名、密码和网站地址。
column_width = [75, 150, 270, 150] for column in range(4): self.grid.setColumnWidth(column, column_width[column])
这几行是设定列的宽度。各列宽度加起来要比窗口宽度稍稍小一些。
headerlabels = ['Website', 'Username', 'Password', 'Url'] self.grid.setHorizontalHeaderLabels(headerlabels)
这两句设定了各列头的名称。
self.grid.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.grid.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
第一句是设定无法修改单元格(因为我们有专门的修改按钮嘛),第二句是设定只能一行一行选择(按行选择有助于修改和删除功能的实现)。
运行一下,效果应该是这样的:
这样的话我们的基本框架就算搭建完成了,下一节我们将进入功能的实现部分。
附各种图标文件:
icon.png:http://download.easyicon.net/png/566430/48/
new.png:http://download.easyicon.net/png/566440/48/
edit.png:http://download.easyicon.net/png/566421/48/
del.png:http://download.easyicon.net/png/566452/48/
【持续更新中】
相关文章推荐
- QtMultiMedia 之 Camera
- QtMultiMedia 之 CameraFocus
- QtMultiMedia 之 CameraImageProcessing
- QML 中的 Qt.include
- Qtcreator中经常使用快捷键总结
- 在ubuntu系统安装配置QT
- win7下安装QT开源库qwt
- 五子棋评分表算法
- QT自定义窗口标题栏实现拖动双击放大缩小
- [视觉算法] 妙算上cuda、opencv以及qt的配置
- qt线程,线程池用到的一点问题
- pyqt中使用matplotlib绘制动态曲线
- Qt国际化
- Qt遍历控件
- Qt获得焦点和失去焦点处理事件 (Focus事件)
- Qt::WA_DeleteOnClose 造成的野指针问题
- Qt5中用QLCDNumber显示时间
- QT 为按钮添加事件
- QT SSL OpenSSL https
- Qt 5.4错误"qt.network.ssl: QSslSocket: cannot call"