使用python代码的pyqt库写一个软件,实现首先有一个登录界面,登录后做一个界面实现数据库管理系统,管理内容为物品管理,分三个表,分别是表一物品明细表、表二为物品使用人员表、表三是物品维修报废时间表,实现表格内容导入,数据库增添删除改变查询
时间: 2024-03-24 22:38:58 浏览: 153
用Python实现数据库编程
好的,我将为你提供一份简单的代码实现。这份代码实现了你所需要的功能,但是仍然需要你自己根据实际需要进行修改和完善。
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox, QGridLayout, QVBoxLayout, QHBoxLayout, QTabWidget, QTableView, QHeaderView, QDialog, QFileDialog, QAction
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QAbstractTableModel, QDate
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
import pandas as pd
import sys
class LoginWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('登录')
self.setWindowIcon(QIcon('icon.png'))
self.username_label = QLabel('用户名:')
self.username_edit = QLineEdit()
self.password_label = QLabel('密码:')
self.password_edit = QLineEdit()
self.password_edit.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton('登录')
self.login_button.clicked.connect(self.login)
self.grid = QGridLayout()
self.grid.addWidget(self.username_label, 0, 0)
self.grid.addWidget(self.username_edit, 0, 1)
self.grid.addWidget(self.password_label, 1, 0)
self.grid.addWidget(self.password_edit, 1, 1)
self.grid.addWidget(self.login_button, 2, 1)
self.setLayout(self.grid)
def login(self):
username = self.username_edit.text()
password = self.password_edit.text()
if username == 'admin' and password == '123456':
self.main_window = MainWindow()
self.main_window.show()
self.close()
else:
QMessageBox.warning(self, '警告', '用户名或密码错误!')
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('物品管理系统')
self.setWindowIcon(QIcon('icon.png'))
self.tab = QTabWidget()
self.tab1 = ItemDetailTable()
self.tab2 = ItemUserTable()
self.tab3 = ItemMaintenanceTable()
self.tab.addTab(self.tab1, '物品明细表')
self.tab.addTab(self.tab2, '物品使用人员表')
self.tab.addTab(self.tab3, '物品维修报废时间表')
self.layout = QVBoxLayout()
self.layout.addWidget(self.tab)
self.setLayout(self.layout)
class ItemDetailTable(QTableView):
def __init__(self):
super().__init__()
self.model = QSqlTableModel()
self.model.setTable('item_detail')
self.model.select()
self.setModel(self.model)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.verticalHeader().setVisible(False)
self.setEditTriggers(QTableView.NoEditTriggers)
self.setSelectionBehavior(QTableView.SelectRows)
self.setSortingEnabled(True)
class ItemUserTable(QTableView):
def __init__(self):
super().__init__()
self.model = QSqlTableModel()
self.model.setTable('item_user')
self.model.select()
self.setModel(self.model)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.verticalHeader().setVisible(False)
self.setEditTriggers(QTableView.NoEditTriggers)
self.setSelectionBehavior(QTableView.SelectRows)
self.setSortingEnabled(True)
class ItemMaintenanceTable(QTableView):
def __init__(self):
super().__init__()
self.model = QSqlTableModel()
self.model.setTable('item_maintenance')
self.model.select()
self.setModel(self.model)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.verticalHeader().setVisible(False)
self.setEditTriggers(QTableView.NoEditTriggers)
self.setSelectionBehavior(QTableView.SelectRows)
self.setSortingEnabled(True)
class ItemModel(QAbstractTableModel):
def __init__(self, data):
super().__init__()
self.data = data
def rowCount(self, parent):
return len(self.data)
def columnCount(self, parent):
return len(self.data.columns)
def data(self, index, role):
if role == Qt.DisplayRole:
return str(self.data.iloc[index.row(), index.column()])
def headerData(self, section, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return str(self.data.columns[section])
class AddItemDialog(QDialog):
def __init__(self, table_name):
super().__init__()
self.setWindowTitle('添加物品')
self.setWindowIcon(QIcon('icon.png'))
self.layout = QVBoxLayout()
self.grid = QGridLayout()
self.button_layout = QHBoxLayout()
self.item_name_label = QLabel('物品名称:')
self.item_name_edit = QLineEdit()
self.item_model_label = QLabel('物品型号:')
self.item_model_edit = QLineEdit()
self.item_price_label = QLabel('物品价格:')
self.item_price_edit = QLineEdit()
self.item_date_label = QLabel('物品购买日期:')
self.item_date_edit = QLineEdit()
self.item_date_edit.setPlaceholderText('YYYY-MM-DD')
self.item_owner_label = QLabel('物品使用人:')
self.item_owner_edit = QLineEdit()
self.item_maintenance_label = QLabel('物品维修日期:')
self.item_maintenance_edit = QLineEdit()
self.item_maintenance_edit.setPlaceholderText('YYYY-MM-DD')
self.item_status_label = QLabel('物品状态:')
self.item_status_edit = QLineEdit()
self.grid.addWidget(self.item_name_label, 0, 0)
self.grid.addWidget(self.item_name_edit, 0, 1)
self.grid.addWidget(self.item_model_label, 1, 0)
self.grid.addWidget(self.item_model_edit, 1, 1)
self.grid.addWidget(self.item_price_label, 2, 0)
self.grid.addWidget(self.item_price_edit, 2, 1)
self.grid.addWidget(self.item_date_label, 3, 0)
self.grid.addWidget(self.item_date_edit, 3, 1)
self.grid.addWidget(self.item_owner_label, 4, 0)
self.grid.addWidget(self.item_owner_edit, 4, 1)
self.grid.addWidget(self.item_maintenance_label, 5, 0)
self.grid.addWidget(self.item_maintenance_edit, 5, 1)
self.grid.addWidget(self.item_status_label, 6, 0)
self.grid.addWidget(self.item_status_edit, 6, 1)
self.save_button = QPushButton('保存')
self.save_button.clicked.connect(lambda: self.save(table_name))
self.cancel_button = QPushButton('取消')
self.cancel_button.clicked.connect(self.close)
self.button_layout.addWidget(self.save_button)
self.button_layout.addWidget(self.cancel_button)
self.layout.addLayout(self.grid)
self.layout.addLayout(self.button_layout)
self.setLayout(self.layout)
def save(self, table_name):
item_name = self.item_name_edit.text()
item_model = self.item_model_edit.text()
item_price = self.item_price_edit.text()
item_date = self.item_date_edit.text()
item_owner = self.item_owner_edit.text()
item_maintenance = self.item_maintenance_edit.text()
item_status = self.item_status_edit.text()
query = QSqlQuery()
query.prepare(f"INSERT INTO {table_name} (item_name, item_model, item_price, item_date, item_owner, item_maintenance, item_status) VALUES (:item_name, :item_model, :item_price, :item_date, :item_owner, :item_maintenance, :item_status)")
query.bindValue(':item_name', item_name)
query.bindValue(':item_model', item_model)
query.bindValue(':item_price', item_price)
query.bindValue(':item_date', item_date)
query.bindValue(':item_owner', item_owner)
query.bindValue(':item_maintenance', item_maintenance)
query.bindValue(':item_status', item_status)
if query.exec_():
QMessageBox.information(self, '提示', '添加成功!')
self.close()
else:
QMessageBox.warning(self, '警告', '添加失败!')
class EditItemDialog(QDialog):
def __init__(self, table_name, item_id):
super().__init__()
self.setWindowTitle('编辑物品')
self.setWindowIcon(QIcon('icon.png'))
self.layout = QVBoxLayout()
self.grid = QGridLayout()
self.button_layout = QHBoxLayout()
self.item_name_label = QLabel('物品名称:')
self.item_name_edit = QLineEdit()
self.item_model_label = QLabel('物品型号:')
self.item_model_edit = QLineEdit()
self.item_price_label = QLabel('物品价格:')
self.item_price_edit = QLineEdit()
self.item_date_label = QLabel('物品购买日期:')
self.item_date_edit = QLineEdit()
self.item_date_edit.setPlaceholderText('YYYY-MM-DD')
self.item_owner_label = QLabel('物品使用人:')
self.item_owner_edit = QLineEdit()
self.item_maintenance_label = QLabel('物品维修日期:')
self.item_maintenance_edit = QLineEdit()
self.item_maintenance_edit.setPlaceholderText('YYYY-MM-DD')
self.item_status_label = QLabel('物品状态:')
self.item_status_edit = QLineEdit()
self.grid.addWidget(self.item_name_label, 0, 0)
self.grid.addWidget(self.item_name_edit, 0, 1)
self.grid.addWidget(self.item_model_label, 1, 0)
self.grid.addWidget(self.item_model
阅读全文