150 lines
5.3 KiB
Python
150 lines
5.3 KiB
Python
|
from PyQt5.QtCore import Qt
|
|||
|
from PyQt5.QtWidgets import QFrame, QLabel, QHBoxLayout, QVBoxLayout, QWidget
|
|||
|
import pyqtgraph as pg
|
|||
|
|
|||
|
|
|||
|
class BoPlotGraph(QWidget): # 继承自 QWidget,减少一层嵌套
|
|||
|
def __init__(self) -> None:
|
|||
|
super().__init__()
|
|||
|
|
|||
|
self.setWindowTitle("血氧监测")
|
|||
|
|
|||
|
# 初始化各个组件
|
|||
|
self.title_widget = self._init_title_widget()
|
|||
|
self.icon_widget = self._init_icon_widget()
|
|||
|
self.value_label = self._init_value_label()
|
|||
|
self.min_label = self._init_min_label()
|
|||
|
|
|||
|
# 设置标题布局
|
|||
|
self.title_layout = QHBoxLayout()
|
|||
|
self.title_layout.addWidget(self.icon_widget, 10) # 占据 10% 的屏幕宽度
|
|||
|
self.title_layout.addWidget(self.title_widget, 90) # 占据 90% 的屏幕宽度
|
|||
|
|
|||
|
# 设置血氧值布局
|
|||
|
self.values_title_layout = QVBoxLayout()
|
|||
|
self.values_title_layout.addWidget(self.value_label, 50) # 占据 50% 的垂直空间
|
|||
|
self.values_title_layout.addWidget(self.min_label, 50) # 占据 50% 的垂直空间
|
|||
|
|
|||
|
# 初始化绘图组件
|
|||
|
self.plot_widget_bo, self.plot_item_bo = self._init_plot_widget()
|
|||
|
self.plot_layout = QHBoxLayout()
|
|||
|
self.plot_layout.addWidget(self.plot_widget_bo)
|
|||
|
|
|||
|
# 设置显示布局
|
|||
|
self.display_layout = QHBoxLayout()
|
|||
|
self.display_layout.addLayout(self.values_title_layout, 40) # 占据 40% 的水平空间
|
|||
|
self.display_layout.addLayout(self.plot_layout, 60) # 占据 60% 的水平空间
|
|||
|
|
|||
|
# 主布局
|
|||
|
main_layout = QVBoxLayout()
|
|||
|
main_layout.addLayout(self.title_layout, 20) # 占据 20% 的垂直空间
|
|||
|
main_layout.addLayout(self.display_layout, 80) # 占据 80% 的垂直空间
|
|||
|
|
|||
|
self.setLayout(main_layout)
|
|||
|
self.setStyleSheet(
|
|||
|
"background-color: #ffffff;"
|
|||
|
"border:1px solid #bbbbbb;"
|
|||
|
"border-radius:4px;"
|
|||
|
"margin-right: 20px;"
|
|||
|
"margin-top:0px"
|
|||
|
)
|
|||
|
|
|||
|
def set_data(self, data, min_data):
|
|||
|
"""
|
|||
|
设置血氧数据和最小血氧值
|
|||
|
:param data: 血氧数据列表
|
|||
|
:param min_data: 最小血氧值
|
|||
|
"""
|
|||
|
self.plot_item_bo.setData(data) # 更新绘图数据
|
|||
|
self.min_label.setText(f"最小血氧:{min_data}%") # 更新最小血氧显示
|
|||
|
if data:
|
|||
|
self.value_label.setText(f'当前血氧:{data[-1]}%') # 更新当前血氧显示
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def _init_icon_widget():
|
|||
|
"""
|
|||
|
初始化图标组件
|
|||
|
:return: 图标 QLabel 对象
|
|||
|
"""
|
|||
|
img = QLabel()
|
|||
|
img.setStyleSheet(
|
|||
|
"border-image: url(assets/bloodO2.png);"
|
|||
|
"background-size: contain;"
|
|||
|
"background-repeat: no-repeat;"
|
|||
|
"background-position: center;"
|
|||
|
)
|
|||
|
img.setAlignment(Qt.AlignCenter) # 图标居中对齐
|
|||
|
img.setScaledContents(True) # 图标大小自动缩放
|
|||
|
return img
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def _init_title_widget():
|
|||
|
"""
|
|||
|
初始化标题组件
|
|||
|
:return: 标题 QLabel 对象
|
|||
|
"""
|
|||
|
title = QLabel("血氧")
|
|||
|
title.setStyleSheet(
|
|||
|
"font: 30px \"Microsoft YaHei UI\";" # 设置字体和大小
|
|||
|
"color: #2c2c2c;" # 设置字体颜色
|
|||
|
"border:none;" # 无边框
|
|||
|
)
|
|||
|
title.setContentsMargins(20, 4, 20, 4) # 设置边距
|
|||
|
return title
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def _init_value_label():
|
|||
|
"""
|
|||
|
初始化当前血氧显示组件
|
|||
|
:return: 当前血氧 QLabel 对象
|
|||
|
"""
|
|||
|
value_label = QLabel('当前血氧:%')
|
|||
|
value_label.setFrameShape(QFrame.Box)
|
|||
|
value_label.setLineWidth(0)
|
|||
|
value_label.setStyleSheet(
|
|||
|
"border:none;"
|
|||
|
"background-color: #ececec;"
|
|||
|
"margin-right: 10px;"
|
|||
|
"margin:0px 0px 0px 0px;"
|
|||
|
"border-radius: 5px;"
|
|||
|
"font: 24px \"Microsoft YaHei UI\";"
|
|||
|
)
|
|||
|
value_label.setAlignment(Qt.AlignCenter) # 文本居中对齐
|
|||
|
return value_label
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def _init_min_label():
|
|||
|
"""
|
|||
|
初始化最小血氧显示组件
|
|||
|
:return: 最小血氧 QLabel 对象
|
|||
|
"""
|
|||
|
min_label = QLabel('最小血氧:%')
|
|||
|
min_label.setFrameShape(QFrame.Box)
|
|||
|
min_label.setLineWidth(0)
|
|||
|
min_label.setStyleSheet(
|
|||
|
"border:none;"
|
|||
|
"background-color: #ececec;"
|
|||
|
"margin-right: 10px;"
|
|||
|
"margin:0px 0px 0px 0px;"
|
|||
|
"border-radius: 5px;"
|
|||
|
"font: 24px \"Microsoft YaHei UI\";"
|
|||
|
)
|
|||
|
min_label.setAlignment(Qt.AlignCenter) # 文本居中对齐
|
|||
|
return min_label
|
|||
|
|
|||
|
@staticmethod
|
|||
|
def _init_plot_widget():
|
|||
|
"""
|
|||
|
初始化绘图组件
|
|||
|
:return: 绘图组件 PlotWidget 和绘图项 PlotDataItem 对象
|
|||
|
"""
|
|||
|
plot_widget_bo = pg.PlotWidget()
|
|||
|
plot_item_bo = plot_widget_bo.plot()
|
|||
|
plot_widget_bo.getAxis('bottom').setTicks([]) # 不显示 x 轴坐标点
|
|||
|
plot_widget_bo.setBackground('w') # 设置白色网格背景
|
|||
|
plot_widget_bo.showGrid(x=True, y=True) # 显示网格
|
|||
|
plot_widget_bo.setYRange(0, 100) # 设置 y 轴范围
|
|||
|
pen = pg.mkPen(color='#5c7bd9', width=3) # 修改线条颜色和粗细
|
|||
|
plot_item_bo.setPen(pen)
|
|||
|
return plot_widget_bo, plot_item_bo
|