# coding: gb2312 import sys from PyQt5.QtCore import Qt from PyQt5.QtGui import QPainter, QColor, QFont, QPen from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout from LSZXPagesLibrary.tool import format_time class CircularProgress(QWidget): def __init__(self): super().__init__() self.rank = 0 self.percentage = 0 self.timer_second = 0 self.state = "<状态>" self.name = "<姓名>" self.hr = 0 self.bo = 0 self.round = 0 def set_data(self, percentage=None, second=None, state=None, name=None, hr=None, bo=None, rank=None, round=None): if percentage: self.percentage = percentage if second: self.timer_second = second if state: self.state = state if name: self.name = name if hr: self.hr = hr if bo: self.bo = bo if rank: self.rank = rank if round: self.round = round def paintEvent(self, event): width, height = 250, 250 self.setMinimumSize(width, height) size = min(width, height) margin = 10 bar_width = 12 painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # 绘制圆形 pen = QPen(QColor(235, 238, 245)) pen.setWidth(bar_width) painter.setPen(pen) painter.drawArc( margin, margin, size - 2 * margin, size - 2 * margin, 0, 360 * 16, ) # 绘制进度 pen.setColor(QColor(50, 150, 250)) painter.setPen(pen) # 将百分比转换为角度,并确保结果为整数 angle = int(-self.percentage * 16 * 3.6) painter.drawArc( margin, margin, size - 2 * margin, size - 2 * margin, 90 * 16, angle, ) # 绘制文字 painter.setFont(QFont('\"Microsoft YaHei UI\"', 20, 600)) # 设置姓名的字体大小为 20px painter.setPen(QPen(QColor(34, 34, 34))) # 设置姓名的颜色为红色 painter.drawText( int(width / 2) - 100, int(height / 2) - 70, 200, 30, Qt.AlignCenter, self.name ) painter.setFont(QFont('\"Microsoft YaHei UI\"', 16)) # 恢复默认的字体大小 painter.setPen(QPen(QColor(255, 0, 0))) # 设置编号和心率血氧的颜色为蓝色 painter.drawText( int(width / 2) - 100, int(height / 2) - 20, 200, 20, Qt.AlignCenter, self.state ) painter.setFont(QFont('\"Microsoft YaHei UI\"', 14)) # 恢复默认的字体大小 painter.setPen(QPen(QColor(153, 153, 153))) # 设置编号和心率血氧的颜色为蓝色 painter.drawText( int(width / 2) - 100, int(height / 2) + 20, 200, 20, Qt.AlignCenter, f"心率: {self.hr}次/分钟" ) painter.setFont(QFont('\"Microsoft YaHei UI\"', 14)) # 恢复默认的字体大小 painter.setPen(QPen(QColor(153, 153, 153))) # 设置编号和心率血氧的颜色为蓝色 painter.drawText( int(width / 2) - 100, int(height / 2) + 50, 200, 20, Qt.AlignCenter, f"血氧: {self.bo}%" ) # painter.drawText( # int(width / 2) - 100, # int(height / 2) + 50, # 200, # 20, # Qt.AlignCenter, # f"心率: {self.hr}, 血氧: {self.bo}" # ) # 绘制顶部框 pen_top = QPen() pen_top.setColor(QColor(187, 187, 187)) painter.setPen(pen_top) top_box_width = int((size - 2 * margin) * 0.6) # 宽度占环形的 50% top_box_height = 30 # 绘制背景矩形 painter.setBrush(QColor(255, 255, 255)) # 设置背景色为白色 painter.drawRect(int(top_box_width * 0.4), 0, top_box_width, top_box_height) # 绘制圆和文字 painter.drawEllipse(int(top_box_width * 0.4), -top_box_height, top_box_width, top_box_height) # 绘制圆 radius_rank = 13 # 小圆的半径 painter.setBrush(QColor(106, 118, 134)) # 设置小圆的背景颜色为红色 painter.drawEllipse(int(top_box_width * 0.48), 2, radius_rank * 2, radius_rank * 2) # 绘制小圆 painter.setFont(QFont('Arial', 14, QFont.Bold)) # 设置字体大小和加粗 painter.setPen(QPen(Qt.white)) # 设置文字颜色为白色 painter.drawText( int(top_box_width * 0.33), 0, int(top_box_width / 2), top_box_height, Qt.AlignCenter, str(self.rank) ) # 绘制文字 # 显示计时 time_str = format_time(int(self.timer_second)) painter.setFont(QFont('Arial', 13)) # 设置字体大小 pen_time = QPen() pen_time.setColor(QColor(44, 62, 80)) painter.setPen(pen_time) # 设置颜色 painter.drawText(int(top_box_width * 0.7), 0, int(top_box_width / 2), top_box_height, Qt.AlignCenter, time_str) # 计算小圆的坐标 radius = 15 # 小圆的半径 # 绘制 45 度角位置的小圆 pen_circle = QPen(Qt.NoPen) # 设置小圆无边框 painter.setPen(pen_circle) painter.setBrush(QColor(245, 108, 108)) # 设置小圆的背景颜色为红色 painter.drawEllipse(int(size - 2 * margin - 40), int(size - 2 * margin - 40), radius * 2, radius * 2) # 绘制小圆 # 在小圆内显示圈数 painter.setFont(QFont('Arial', 14)) # 设置字体大小 painter.setPen(QPen(Qt.white)) # 设置文字颜色为蓝色 painter.drawText(int(size - 2 * margin - 40), int(size - 2 * margin - 40), radius * 2, radius * 2, Qt.AlignCenter, str(self.round)) # 绘制文字