使用python代码的pyqt库做一个软件,实现首先有一个登录界面,登录后做一个界面实现数据库管理系统,实现内容导入,数据库增删改查
时间: 2024-03-24 11:38:46 浏览: 102
基于Python实现的快递管理系统源码+数据库+PyQt6实现GUI界面(高分项目)
好的,以下是一个简单的实现代码,具体的实现过程中需要根据实际需求进行调整和优化:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QComboBox, QFileDialog, QMessageBox
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
import sys
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('登录')
self.resize(300, 200)
self.username_label = QLabel('用户名:', self)
self.username_label.move(50, 50)
self.username_edit = QLineEdit(self)
self.username_edit.move(100, 50)
self.password_label = QLabel('密码:', self)
self.password_label.move(50, 100)
self.password_edit = QLineEdit(self)
self.password_edit.setEchoMode(QLineEdit.Password)
self.password_edit.move(100, 100)
self.login_button = QPushButton('登录', self)
self.login_button.move(100, 150)
self.login_button.clicked.connect(self.login)
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
# 进行用户名和密码验证
if username == 'admin' and password == '123456':
self.hide()
self.main_window = MainWindow()
self.main_window.show()
else:
QMessageBox.warning(self, '错误', '用户名或密码错误!')
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('数据库管理系统')
self.resize(800, 600)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.database_label = QLabel('选择数据库:', self.central_widget)
self.database_label.move(50, 50)
self.database_combo = QComboBox(self.central_widget)
self.database_combo.move(150, 50)
self.database_combo.addItems(['db1', 'db2', 'db3'])
self.database_combo.currentIndexChanged.connect(self.select_database)
self.table_label = QLabel('选择表格:', self.central_widget)
self.table_label.move(50, 100)
self.table_combo = QComboBox(self.central_widget)
self.table_combo.move(150, 100)
self.table_combo.currentIndexChanged.connect(self.select_table)
self.table_widget = QTableWidget(self.central_widget)
self.table_widget.move(50, 150)
self.table_widget.setColumnCount(3)
self.table_widget.setHorizontalHeaderLabels(['ID', '姓名', '年龄'])
self.import_button = QPushButton('导入数据', self.central_widget)
self.import_button.move(50, 400)
self.import_button.clicked.connect(self.import_data)
self.add_button = QPushButton('添加数据', self.central_widget)
self.add_button.move(200, 400)
self.add_button.clicked.connect(self.add_data)
self.modify_button = QPushButton('修改数据', self.central_widget)
self.modify_button.move(350, 400)
self.modify_button.clicked.connect(self.modify_data)
self.delete_button = QPushButton('删除数据', self.central_widget)
self.delete_button.move(500, 400)
self.delete_button.clicked.connect(self.delete_data)
self.init_database()
def init_database(self):
# 初始化数据库连接
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('test.db')
if not self.db.open():
QMessageBox.warning(self, '错误', '无法打开数据库!')
return
# 加载数据库表格
query = QSqlQuery()
query.exec_('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
query.exec_('INSERT INTO test(id, name, age) VALUES(1, "张三", 20)')
query.exec_('INSERT INTO test(id, name, age) VALUES(2, "李四", 25)')
query.exec_('INSERT INTO test(id, name, age) VALUES(3, "王五", 30)')
self.select_database(0)
def select_database(self, index):
# 选择数据库
database_name = self.database_combo.currentText()
self.db.setDatabaseName(database_name + '.db')
# 重新加载数据库表格
self.select_table(0)
def select_table(self, index):
# 选择表格
table_name = self.table_combo.currentText()
query = QSqlQuery()
query.exec_('SELECT * FROM {}'.format(table_name))
self.table_widget.setRowCount(0)
while query.next():
row = self.table_widget.rowCount()
self.table_widget.insertRow(row)
for i in range(3):
item = QTableWidgetItem(str(query.value(i)))
self.table_widget.setItem(row, i, item)
def import_data(self):
# 导入数据
file_name, _ = QFileDialog.getOpenFileName(self, '选择文件', '', 'CSV Files (*.csv)')
if file_name:
with open(file_name, 'r') as f:
data = f.readlines()
query = QSqlQuery()
for line in data:
line = line.strip().split(',')
query.exec_('INSERT INTO test(name, age) VALUES("{}", {})'.format(line[0], line[1]))
self.select_table(0)
def add_data(self):
# 添加数据
query = QSqlQuery()
query.exec_('INSERT INTO test(name, age) VALUES("新用户", 0)')
self.select_table(0)
def modify_data(self):
# 修改数据
rows = self.table_widget.selectedItems()
if len(rows) == 0:
QMessageBox.warning(self, '错误', '请选择要修改的数据!')
return
row = rows[0].row()
id = self.table_widget.item(row, 0).text()
name = self.table_widget.item(row, 1).text()
age = self.table_widget.item(row, 2).text()
query = QSqlQuery()
query.exec_('UPDATE test SET name="{}", age={} WHERE id={}'.format(name, age, id))
self.select_table(0)
def delete_data(self):
# 删除数据
rows = self.table_widget.selectedItems()
if len(rows) == 0:
QMessageBox.warning(self, '错误', '请选择要删除的数据!')
return
row = rows[0].row()
id = self.table_widget.item(row, 0).text()
query = QSqlQuery()
query.exec_('DELETE FROM test WHERE id={}'.format(id))
self.select_table(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
login_window = LoginWindow()
login_window.show()
sys.exit(app.exec_())
```
运行以上代码,即可实现一个简单的登录界面和数据库管理系统。需要注意的是,该代码中使用了SQLite作为数据库,需要安装PyQt5和PyQt5-sqlite模块,并且需要将该代码保存为.py文件并运行。同时,还需要考虑数据的安全性、用户体验等问题。
阅读全文