使用pyqt5设计一个窗口,页面包含不同的页面,有材料,装备,设计图,药品,宠物,其他等页面,显示的数据包含物品显示,名字,类型,数量,属性
时间: 2023-10-29 17:06:38 浏览: 86
人工智能-项目实践-获取3775家上市公司近5年财务报表,建立财务报表分析模型,借助pyqt生成窗口化展示页面
以下是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QTableWidget, QTableWidgetItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("物品管理系统")
self.resize(800, 600)
# 创建主窗口中心的容器
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
# 创建页面切换按钮
self.materials_btn = QLabel("材料")
self.equipment_btn = QLabel("装备")
self.design_btn = QLabel("设计图")
self.medicine_btn = QLabel("药品")
self.pet_btn = QLabel("宠物")
self.other_btn = QLabel("其他")
# 创建页面容器
self.materials_page = QWidget()
self.equipment_page = QWidget()
self.design_page = QWidget()
self.medicine_page = QWidget()
self.pet_page = QWidget()
self.other_page = QWidget()
# 创建页面中的表格
self.materials_table = QTableWidget()
self.equipment_table = QTableWidget()
self.design_table = QTableWidget()
self.medicine_table = QTableWidget()
self.pet_table = QTableWidget()
self.other_table = QTableWidget()
# 设置表格列数和列名
self.materials_table.setColumnCount(4)
self.materials_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
self.equipment_table.setColumnCount(4)
self.equipment_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
self.design_table.setColumnCount(4)
self.design_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
self.medicine_table.setColumnCount(4)
self.medicine_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
self.pet_table.setColumnCount(4)
self.pet_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
self.other_table.setColumnCount(4)
self.other_table.setHorizontalHeaderLabels(["名称", "类型", "数量", "属性"])
# 向表格中添加数据
self.add_data_to_table(self.materials_table, [["钢铁", "材料", "50", "耐久度+10"], ["黄金", "材料", "5", "价值+1000"]])
self.add_data_to_table(self.equipment_table, [["铁剑", "武器", "1", "攻击力+10"], ["皮甲", "防具", "1", "防御力+5"]])
self.add_data_to_table(self.design_table, [["蓝图:飞行器", "设计图", "1", "需要材料:钢铁x20,黄金x10"]])
self.add_data_to_table(self.medicine_table, [["红药水", "药品", "10", "恢复生命值+50"], ["蓝药水", "药品", "5", "恢复魔法值+50"]])
self.add_data_to_table(self.pet_table, [["小猫", "宠物", "1", "攻击力+5,防御力+5"]])
self.add_data_to_table(self.other_table, [["钥匙", "其他", "3", "开启宝箱"], ["地图", "其他", "1", "显示全局地图"]])
# 将表格添加到页面容器中
self.add_table_to_page(self.materials_table, self.materials_page)
self.add_table_to_page(self.equipment_table, self.equipment_page)
self.add_table_to_page(self.design_table, self.design_page)
self.add_table_to_page(self.medicine_table, self.medicine_page)
self.add_table_to_page(self.pet_table, self.pet_page)
self.add_table_to_page(self.other_table, self.other_page)
# 创建页面容器的垂直布局
self.materials_layout = QVBoxLayout()
self.equipment_layout = QVBoxLayout()
self.design_layout = QVBoxLayout()
self.medicine_layout = QVBoxLayout()
self.pet_layout = QVBoxLayout()
self.other_layout = QVBoxLayout()
# 将页面容器添加到垂直布局中
self.materials_layout.addWidget(self.materials_table)
self.equipment_layout.addWidget(self.equipment_table)
self.design_layout.addWidget(self.design_table)
self.medicine_layout.addWidget(self.medicine_table)
self.pet_layout.addWidget(self.pet_table)
self.other_layout.addWidget(self.other_table)
# 设置页面容器的布局
self.materials_page.setLayout(self.materials_layout)
self.equipment_page.setLayout(self.equipment_layout)
self.design_page.setLayout(self.design_layout)
self.medicine_page.setLayout(self.medicine_layout)
self.pet_page.setLayout(self.pet_layout)
self.other_page.setLayout(self.other_layout)
# 创建页面切换容器的水平布局
self.btn_layout = QHBoxLayout()
self.btn_layout.addWidget(self.materials_btn)
self.btn_layout.addWidget(self.equipment_btn)
self.btn_layout.addWidget(self.design_btn)
self.btn_layout.addWidget(self.medicine_btn)
self.btn_layout.addWidget(self.pet_btn)
self.btn_layout.addWidget(self.other_btn)
# 将页面切换容器和页面容器添加到主窗口中心的垂直布局中
self.central_layout = QVBoxLayout()
self.central_layout.addLayout(self.btn_layout)
self.central_layout.addWidget(self.materials_page)
self.central_layout.addWidget(self.equipment_page)
self.central_layout.addWidget(self.design_page)
self.central_layout.addWidget(self.medicine_page)
self.central_layout.addWidget(self.pet_page)
self.central_layout.addWidget(self.other_page)
self.central_widget.setLayout(self.central_layout)
# 连接页面切换按钮的信号和槽函数
self.materials_btn.mousePressEvent = lambda event:self.show_page(self.materials_page)
self.equipment_btn.mousePressEvent = lambda event:self.show_page(self.equipment_page)
self.design_btn.mousePressEvent = lambda event:self.show_page(self.design_page)
self.medicine_btn.mousePressEvent = lambda event:self.show_page(self.medicine_page)
self.pet_btn.mousePressEvent = lambda event:self.show_page(self.pet_page)
self.other_btn.mousePressEvent = lambda event:self.show_page(self.other_page)
# 默认显示第一个页面
self.show_page(self.materials_page)
# 向表格中添加数据
def add_data_to_table(self, table, data):
table.setRowCount(len(data))
for i, row in enumerate(data):
for j, item in enumerate(row):
table.setItem(i, j, QTableWidgetItem(item))
# 将表格添加到页面容器中
def add_table_to_page(self, table, page):
layout = QVBoxLayout()
layout.addWidget(table)
page.setLayout(layout)
# 显示指定的页面
def show_page(self, page):
for child in self.central_widget.children():
if isinstance(child, QWidget):
child.setVisible(child == page)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
运行后,界面如下图所示:
![物品管理系统界面截图](https://img-blog.csdnimg.cn/20210721162329334.png)
阅读全文