from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QTableWidget, QTableWidgetItem, QFileDialog, QPrintDialog
from PySide6.QtCore import Qt, QAbstractTableModel
from PySide6.QtGui import QPrinter, QPainter
import sys
import openpyxl
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class PersonTableModel(QAbstractTableModel):
def __init__(self, persons):
super().__init__()
self.persons = persons
self.headers = ['姓名', '年龄', '性别']
def rowCount(self, parent=None):
return len(self.persons)
def columnCount(self, parent=None):
return len(self.headers)
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
row = index.row()
col = index.column()
if role == Qt.DisplayRole:
person = self.persons[row]
if col == 0:
return person.name
elif col == 1:
return person.age
elif col == 2:
return person.gender
return None
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.headers[section]
return None
class PersonManagementSystem(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("人员管理系统")
self.persons = []
self.model = PersonTableModel(self.persons)
self.tableView = QTableWidget()
self.tableView.setModel(self.model)
self.name_label = QLabel("姓名:")
self.name_edit = QLineEdit()
self.age_label = QLabel("年龄:")
self.age_edit = QLineEdit()
self.gender_label = QLabel("性别:")
self.gender_edit = QLineEdit()
self.add_button = QPushButton("添加")
self.export_button = QPushButton("导出至xlsx")
self.print_button = QPushButton("打印")
self.delete_button = QPushButton("删除")
self.init_ui()
def init_ui(self):
widget = QWidget()
layout = QVBoxLayout(widget)
form_layout = QHBoxLayout()
form_layout.addWidget(self.name_label)
form_layout.addWidget(self.name_edit)
form_layout.addWidget(self.age_label)
form_layout.addWidget(self.age_edit)
form_layout.addWidget(self.gender_label)
form_layout.addWidget(self.gender_edit)
form_layout.addWidget(self.add_button)
form_layout.addWidget(self.export_button)
form_layout.addWidget(self.print_button)
layout.addLayout(form_layout)
layout.addWidget(self.tableView)
layout.addWidget(self.delete_button)
self.setCentralWidget(widget)
self.add_button.clicked.connect(self.add_person)
self.export_button.clicked.connect(self.export_to_xlsx)
self.print_button.clicked.connect(self.print_table)
self.delete_button.clicked.connect(self.delete_person)
def add_person(self):
name = self.name_edit.text()
age = self.age_edit.text()
gender = self.gender_edit.text()
if not name or not age or not gender:
QMessageBox.warning(self, "警告", "请输入完整信息")
return
person = Person(name, age, gender)
self.persons.append(person)
self.model.layoutChanged.emit()
self.name_edit.clear()
self.age_edit.clear()
self.gender_edit.clear()
def export_to_xlsx(self):
file_name, _ = QFileDialog.getSaveFileName(self, "导出至xlsx", "", "Excel Files (*.xlsx)")
if not file_name:
return
workbook = openpyxl.Workbook()
worksheet = workbook.active
for col, header in enumerate(self.model.headers, start=1):
header_cell = worksheet.cell(row=1, column=col)
header_cell.value = header
for row, person in enumerate(self.persons, start=2):
worksheet.cell(row=row, column=1).value = person.name
worksheet.cell(row=row, column=2).value = person.age
worksheet.cell(row=row, column=3).value = person.gender
workbook.save(file_name)
QMessageBox.information(self, "成功", "导出成功!")
def print_table(self):
printer = QPrinter(QPrinter.HighResolution)
dialog = QPrintDialog(printer, self)
if dialog.exec() == QPrintDialog.Accepted:
painter = QPainter(printer)
self.tableView.render(painter)
painter.end()
def delete_person(self):
selected_row = self.tableView.currentIndex().row()
if selected_row == -1:
QMessageBox.warning(self, "警告", "请选择要删除的人员")
return
confirm = QMessageBox.question(self, "确认", "确定删除选中的人员吗?", QMessageBox.Yes | QMessageBox.No)
if confirm == QMessageBox.Yes:
del self.persons[selected_row]
self.model.layoutChanged.emit()
self.tableView.clearSelection()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = PersonManagementSystem()
window.show()
sys.exit(app.exec())