简介
这是一个操作数据库(sqlite3)的项目,用PyQt5进行界面封装。
此次项目最主要的是,主界面与子界面的交互,一个主界面与三个子界面交互
可以添加数据,修改数据,删除数据
主界面
class MainWidget(QMainWindow):
def __init__(self):
super(MainWidget,self).__init__()
self.ui_init()
self.connect_init()
self.sql_init()
def ui_init(self):
self.setWindowTitle('信息查询')
self.resize(600,700)
self.addwidget = AddWidget()
self.deletewidget = DeleteWidget()
self.updatewidget = UpdateWidget()
self.show_view = QTextBrowser()
self.find_text = QLineEdit()
self.find_btn = QPushButton()
self.find_btn.setText('查询')
self.update_btn = QPushButton()
self.update_btn.setText('更改')
self.delete_btn = QPushButton()
self.delete_btn.setText('删除')
self.add_btn = QPushButton()
self.add_btn.setText('添加')
self.status_bar = self.statusBar()
self.widget = QWidget()
self.h1_layout = QHBoxLayout()
self.h2_layout = QHBoxLayout()
self.v_layout = QVBoxLayout()
self.h1_layout.addWidget(self.find_text)
self.h1_layout.addWidget(self.find_btn)
self.h2_layout.addWidget(self.update_btn)
self.h2_layout.addWidget(self.delete_btn)
self.h2_layout.addWidget(self.add_btn)
self.v_layout.addLayout(self.h1_layout)
self.v_layout.addLayout(self.h2_layout)
self.v_layout.addWidget(self.show_view)
self.widget.setLayout(self.v_layout)
self.setCentralWidget(self.widget)
def connect_init(self):
'''
信号与槽
'''
'''查询功能'''
self.find_btn.clicked.connect(lambda:self.find_slot(self.find_text.text()))
'''添加功能'''
self.add_btn.clicked.connect(self.add_slot)
self.addwidget.add_close_signal.connect(lambda:self.setEnabled(True))
self.addwidget.add_start_signal.connect(self.add_start_slot)
'''删除功能'''
self.delete_btn.clicked.connect(self.delete_slot)
self.deletewidget.delete_close_signal.connect(lambda:self.setEnabled(True))
self.deletewidget.delete_start_signal.connect(self.delete_start_slot)
'''更新功能'''
self.update_btn.clicked.connect(self.update_slot)
self.updatewidget.update_close_signal.connect(lambda:self.setEnabled(True))
self.updatewidget.update_name_signal.connect(self.update_name_slot)
self.updatewidget.update_show_signal.connect(self.update_show_slot)
def sql_init(self):
'''
sqllite3初始化
'''
try:
conn = sqlite3.connect('find.db')
cursor = conn.cursor()
cursor.execute('select * from design')
cursor.close()
conn.close()
except:
conn = sqlite3.connect('find.db')
cursor = conn.cursor()
cursor.execute('create table design (id integer PRIMARY KEY AUTOINCREMENT,name varchar(20),show varchar(100))')
cursor.close()
conn.close()
'''查询槽'''
def find_slot(self,content):
try:
result = self.find_func(content)
self.status_bar.showMessage('查询成功')
if result:
self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
for i in range(len(result)):
self.show_view.append('id:' + str(result[i][0]))
self.show_view.append('名称:' + result[i][1])
self.show_view.append('说明:' + result[i][2])
else:
self.show_view.append('<font size="5" color="red">共查询 {} 条数据</font>'.format(len(result)))
except Exception as e:
self.status_bar.showMessage('查询失败')
print(e)
'''添加槽'''
def add_slot(self):
self.addwidget.show()
self.setEnabled(False)
def add_start_slot(self,name,show):
try:
self.insert_func(name,show)
self.status_bar.showMessage('添加成功')
except Exception as e:
self.status_bar.showMessage('添加失败')
self.setEnabled(True)
self.addwidget.name_text.clear()
self.addwidget.show_text.clear()
self.addwidget.close()
'''删除槽'''
def delete_slot(self):
self.deletewidget.show()
self.setEnabled(False)
def delete_start_slot(self,id):
try:
self.delete_func(id)
self.reset_func()
self.status_bar.showMessage('删除成功')
except Exception as e:
self.status_bar.showMessage('删除失败')
self.setEnabled(True)
self.deletewidget.close()
'''更新槽'''
def update_slot(self):
self.updatewidget.show()
self.setEnabled(False)
def update_name_slot(self,id,name):
try:
self.update_name_func(id,name)
self.status_bar.showMessage('更新名称成功')
except Exception as e:
self.status_bar.showMessage('更新名称失败')
self.setEnabled(True)
self.updatewidget.close()
def update_show_slot(self,id,name):
try:
self.update_show_func(id,name)
self.status_bar.showMessage('更新名称成功')
except Exception as e:
self.status_bar.showMessage('更新名称失败')
self.setEnabled(True)
self.updatewidget.close()
'''数据库函数'''
def insert_func(self,name,show):
'''
插入信息
'''
conn = sqlite3.connect('find.db')
cursor = conn.cursor()
cursor.execute('insert into design (id,name,show) values (null,"{}","{}")'.format(name,show))
cursor.close()
conn.commit()
conn.close()
def find_func