之前用python写了个小软件https://blog.51cto.com/ikezcn/2142638
说实话用tk的话界面确实不好看,所以需要改进,看了pyqt5的介绍想要试一下,对于上次写的软件来说这里只是更改了界面而已。上面的程序不再更新。
使用的软件:
python3.6
pyqt5 安装:pip insatll pyqt5
界面:
代码(干货来了,之后会不定时更新):
# -*- coding: utf-8 -*-
#v2.0 2018-08-30更新
#v2.1 2018-08-31更新
#现在可以使用pyinsatller打包了,新增了样式表,就算没有数据库也会自己创建并增加数据表,隐藏列名
#v2.2 2018-09-05更新
#增加了左边一列用来做查询条件,双击就可以搜索.
#右键右边的路径可以添加搜索条件.
#最后遇到一个问题:多次使用setFilter会造成模型出现问题导致无法写入数据库,这里就直接跳过模型自己插入数据库
#v2.3 2018-09-06更新
#增加刷新菜单,显示所有路径,修复bug
#v2.4 2018-09-11更新
#小修小补
import sys,os
from PyQt5 import sip
from PyQt5.QtCore import Qt,QVariant,QFile,QIODevice,QItemSelectionModel
from PyQt5.QtSql import QSqlDatabase,QSqlTableModel,QSqlQuery,QSqlRecord
from PyQt5.QtWidgets import QPushButton,QLabel,QCheckBox,QHBoxLayout,QVBoxLayout,QWidget,QMenu,QMainWindow,QMessageBox,QHeaderView,QAction, qApp, QApplication, QDesktopWidget , QGridLayout,QTableView,QAbstractItemView
from PyQt5.QtGui import QIcon,QContextMenuEvent
import win32api
import win32con
class Icon(QMainWindow):
def __init__(self,model,sqlite,query,view,sview,smodel):
super().__init__()
self.model = model
self.sqlite = sqlite
self.query = query
self.view = view
self.sview = sview
self.smodel = smodel
if self.sqlite.open():#如果没有找到数据库文件就自动创建
self.query.exec("select count(*) from sqlite_master where type='table' and name='lj'")
if self.query.next():
if self.query.value(0) == 0:
self.query.prepare("CREATE TABLE lj(id integer primary key,lj text not NULL,isdel BOOLEAN DEFAULT 0)")
if not self.query.exec():
QMessageBox.information(self,"数据表创建失败",self.query.lastError().text())
self.query.prepare("CREATE TABLE cxtj(cxtj text not NULL)")
if not self.query.exec():
QMessageBox.information(self,"数据表创建失败",self.query.lastError().text())
else:
QMessageBox.information(self,"程序将关闭",self.sqlite.lastError().text())
sys.exit(app.exec_())
self.setAcceptDrops(True)
self.statusBar().showMessage('Reday')
delAction = QAction("&删除", self)
delAction.setShortcut("Ctrl+D")
delAction.setStatusTip("删除选定的行")
delAction.triggered.connect(self.delrow)
deltableAction = QAction("&全部删除", self)
deltableAction.setStatusTip("删除所有的行")
deltableAction.triggered.connect(self.deltable)
vacuumAction = QAction("&整理数据库", self)
vacuumAction.setShortcut("Ctrl+D")
vacuumAction.setStatusTip("让数据库变小")
vacuumAction.triggered.connect(self.vacuum)
selectAction = QAction("&刷新", self)
selectAction.setShortcut("Ctrl+R")
selectAction.setStatusT