回复 楼主 yll148
以下是一个简单的报表管理小程序,使用PyQt5实现。该程序允许用户添加、编辑和删除报表,以及查看报表的详细信息。
程序代码:
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox
from PyQt5.QtCore import Qt
class Report:
def __init__(self, name, description, date):
self.name = name
self.description = description
self.date = date
class ReportManager(QMainWindow):
def __init__(self):
super().__init__()
# 初始化报表列表
self.reports = []
# 设置窗口标题
self.setWindowTitle('Report Manager')
# 创建主窗口部件
main_widget = QWidget()
self.setCentralWidget(main_widget)
# 创建主窗口布局
main_layout = QVBoxLayout()
main_widget.setLayout(main_layout)
# 创建报表列表部件
report_list_widget = QWidget()
main_layout.addWidget(report_list_widget)
# 创建报表列表布局
report_list_layout = QHBoxLayout()
report_list_widget.setLayout(report_list_layout)
# 创建报表列表
self.report_table = QTableWidget()
self.report_table.setColumnCount(3)
self.report_table.setHorizontalHeaderLabels(['Name', 'Description', 'Date'])
self.report_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.report_table.verticalHeader().setVisible(False)
report_list_layout.addWidget(self.report_table)
# 创建报表操作部件
report_action_widget = QWidget()
main_layout.addWidget(report_action_widget)
# 创建报表操作布局
report_action_layout = QHBoxLayout()
report_action_widget.setLayout(report_action_layout)
# 创建报表名称标签和文本框
name_label = QLabel('Name:')
self.name_edit = QLineEdit()
report_action_layout.addWidget(name_label)
report_action_layout.addWidget(self.name_edit)
# 创建报表描述标签和文本框
description_label = QLabel('Description:')
self.description_edit = QLineEdit()
report_action_layout.addWidget(description_label)
report_action_layout.addWidget(self.description_edit)
# 创建报表日期标签和文本框
date_label = QLabel('Date:')
self.date_edit = QLineEdit()
report_action_layout.addWidget(date_label)
report_action_layout.addWidget(self.date_edit)
# 创建添加报表按钮
add_button = QPushButton('Add')
add_button.clicked.connect(self.add_report)
report_action_layout.addWidget(add_button)
# 创建编辑报表按钮
edit_button = QPushButton('Edit')
edit_button.clicked.connect(self.edit_report)
report_action_layout.addWidget(edit_button)
# 创建删除报表按钮
delete_button = QPushButton('Delete')
delete_button.clicked.connect(self.delete_report)
report_action_layout.addWidget(delete_button)
# 更新报表列表
self.update_report_table()
def update_report_table(self):
# 清空报表列表
self.report_table.setRowCount(0)
# 添加报表到列表
for report in self.reports:
row = self.report_table.rowCount()
self.report_table.insertRow(row)
self.report_table.setItem(row, 0, QTableWidgetItem(report.name))
self.report_table.setItem(row, 1, QTableWidgetItem(report.description))
self.report_table.setItem(row, 2, QTableWidgetItem(report.date))
def add_report(self):
# 获取报表信息
name = self.name_edit.text()
description = self.description_edit.text()
date = self.date_edit.text()
# 创建报表
report = Report(name, description, date)
# 添加报表到列表
self.reports.append(report)
# 更新报表列表
self.update_report_table()
# 清空文本框
self.name_edit.clear()
self.description_edit.clear()
self.date_edit.clear()
def edit_report(self):
# 获取当前选中的报表
selected_rows = self.report_table.selectionModel().selectedRows()
# 如果没有选中报表,弹出提示框
if len(selected_rows) == 0:
QMessageBox.warning(self, 'Warning', 'Please select a report to edit.')
return
# 获取选中报表的索引
selected_row = selected_rows[0].row()
# 获取选中报表的信息
name = self.name_edit.text()
description = self.description_edit.text()
date = self.date_edit.text()
# 更新选中报表的信息
self.reports[selected_row].name = name
self.reports[selected_row].description = description
self.reports[selected_row].date = date
# 更新报表列表
self.update_report_table()
# 清空文本框
self.name_edit.clear