编写一个基于python+pyqt5+mysql的具有三层架构的信息管理项目代码
时间: 2023-11-12 07:06:36 浏览: 78
很抱歉,我不能直接为您提供完整的代码。但是,我可以给您提供一个基于Python+PyQt5+MySQL的三层架构的信息管理项目的代码框架:
1. 数据库层
```
import mysql.connector
class Database:
def __init__(self):
self.conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="database_name"
)
self.cursor = self.conn.cursor()
def execute(self, query, params=None):
self.cursor.execute(query, params)
self.conn.commit()
def query(self, query, params=None):
self.cursor.execute(query, params)
return self.cursor.fetchall()
```
2. 逻辑层
```
class Logic:
def __init__(self):
self.db = Database()
def add_item(self, item):
query = "INSERT INTO items (name, description) VALUES (%s, %s)"
params = (item["name"], item["description"])
self.db.execute(query, params)
def get_items(self):
query = "SELECT * FROM items"
return self.db.query(query)
```
3. 用户界面层
```
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton, QListWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Information Management")
main_widget = QWidget()
main_layout = QHBoxLayout()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
# Left panel - input form
left_widget = QWidget()
left_layout = QVBoxLayout()
left_widget.setLayout(left_layout)
main_layout.addWidget(left_widget)
name_label = QLabel("Name:")
left_layout.addWidget(name_label)
self.name_input = QLineEdit()
left_layout.addWidget(self.name_input)
description_label = QLabel("Description:")
left_layout.addWidget(description_label)
self.description_input = QTextEdit()
left_layout.addWidget(self.description_input)
add_button = QPushButton("Add")
add_button.clicked.connect(self.add_item)
left_layout.addWidget(add_button)
# Right panel - item list
right_widget = QWidget()
right_layout = QVBoxLayout()
right_widget.setLayout(right_layout)
main_layout.addWidget(right_widget)
list_label = QLabel("Items:")
right_layout.addWidget(list_label)
self.item_list = QListWidget()
right_layout.addWidget(self.item_list)
self.load_items()
def add_item(self):
item = {
"name": self.name_input.text(),
"description": self.description_input.toPlainText()
}
logic = Logic()
logic.add_item(item)
self.load_items()
def load_items(self):
self.item_list.clear()
logic = Logic()
items = logic.get_items()
for item in items:
self.item_list.addItem(item[1])
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
请注意,这只是一个简单的框架,您需要根据您的具体需求进行调整和完善。
阅读全文