第一步:装PyQt库
pip install PyQt5
第二步:复制代码
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout,
QWidget, QLabel, QProgressBar, QSlider, QLineEdit, QHBoxLayout)
from PyQt5.QtCore import QTimer, Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
# 心脏起搏器模拟器
class PacemakerSimulator(QMainWindow):
def __init__(self, ecg_data):
super().__init__()
self.pulse_width = 200 # 起搏器脉宽的初始值(微秒)
self.pulse_rate = 60 # 起搏器脉冲频率的初始值(每分钟脉冲数)
self.battery_level = 100 # 起搏器的初始电池电量(百分比)
self.ecg_data = ecg_data # 添加实际ECG数据
self.current_ecg_index = 0 # 初始化ECG数据索引
self.ecg_plot_length = 50 # 设置ECG波形的长度
self.initUI()
# 使用QTimer模拟心电图更新和起搏器脉冲
self.ecg_timer = QTimer(self)
self.ecg_timer.timeout.connect(self.update_ecg)
self.pacing_timer = QTimer(self)
self.pacing_timer.timeout.connect(self.pace)
def initUI(self):
# 设置窗口
self.setWindowTitle('心脏起搏器模拟器')
self.setGeometry(300, 300, 800, 600)
# 创建布局和控件
layout = QVBoxLayout()
# 患者信息
patient_info_layout = QHBoxLayout()
self.patient_name_label = QLabel('患者姓名:')
self.patient_name_input = QLineEdit(self)
self.patient_name_input.setText("John Doe")
patient_info_layout.addWidget(self.patient_name_label)
patient_info_layout.addWidget(self.patient_name_input)
layout.addLayout(patient_info_layout)
# 电池电量
self.battery_label = QLabel('电池电量:')
self.battery_progress = QProgressBar(self)
self.battery_progress.setValue(self.battery_level)
layout.addWidget(self.battery_label)
layout.addWidget(self.battery_progress)
# 心电图
self.ecg_figure = Figure()
self.ecg_canvas = FigureCanvas(self.ecg_figure)
self.ecg_plot = self.ecg_figure.add_subplot(111)
self.ecg_plot.axis('off') # 可以选择是否显示坐标轴
layout.addWidget(self.ecg_canvas)
# 脉冲频率控制
self.pulse_rate_label = QLabel(f'脉冲频率: {self.pulse_rate} BPM')
self.pulse_rate_slider = QSlider(Qt.Horizontal, self)
self.pulse_rate_slider.setMinimum(30)
self.pulse_rate_slider.setMaximum(120)
self.pulse_rate_slider.setValue(self.pulse_rate)
self.pulse_rate_slider.valueChanged.connect(self.change_rate)
layout.addWidget(self.pulse_rate_label)
layout.addWidget(self.pulse_rate_slider)
# 起搏器控制按钮
self.start_button = QPushButton('启动', self)
self.stop_button = QPushButton('停止', self)
self.stop_button.setEnabled(False) # 初始时停止按钮不可用
layout.addWidget(self.start_button)
layout.addWidget(self.stop_button)
# 设置中心窗口
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# 连接按钮的点击事件
self.start_button.clicked.connect(self.start_pacing)
self.stop_button.clicked.connect(self.stop_pacing)
def start_pacing(self):
pulse_interval_ms = int(60000 / self.pulse_rate) # 将结果转换为整数
self.pacing_timer.start(pulse_interval_ms)
self.ecg_timer.start(100) # 心电图更新频率为 100 ms
self.start_button.setEnabled(False)
self.stop_button.setEnabled(True)
def stop_pacing(self):
self.pacing_timer.stop()
self.ecg_timer.stop()
self.start_button.setEnabled(True)
self.stop_button.setEnabled(False)
def pace(self):
# 模拟脉冲发射
self.battery_level -= 0.1
self.battery_progress.setValue(int(self.battery_level))
if self.battery_level <= 0:
self.battery_level = 100 # 假设可以充电
def change_rate(self, value):
self.pulse_rate = value
self.pulse_rate_label.setText(f'脉冲频率: {self.pulse_rate} BPM')
if self.pacing_timer.isActive():
pulse_interval_ms = 60000 / self.pulse_rate
self.pacing_timer.start(pulse_interval_ms) # 更新起搏器发射脉冲的频率
def update_ecg(self):
# 更新心电图数据并显示
if self.current_ecg_index + self.ecg_plot_length > len(self.ecg_data):
self.current_ecg_index = 0 # 如果到达数据末尾,重新开始
self.ecg_plot.clear()
self.ecg_plot.plot(self.ecg_data[self.current_ecg_index:self.current_ecg_index+self.ecg_plot_length], '-r')
self.ecg_plot.axis('off') # 关闭坐标轴
self.ecg_canvas.draw()
self.current_ecg_index += 1
def main():
ecg_data = [
1822, 1793, 1814, 1670, 1684, 1660, 1552, 1503, 1565, 1508, 1454, 1534, 1479, 1414, 1563, 1563, 1471, 1602,
1584, 1522, 1589, 1564, 1454, 1485, 1568, 1461, 1424, 1527, 1488, 1437, 1631, 1554, 1456, 1565, 1552, 1439,
1551, 1568, 1543, 1533, 1643, 1610, 1633, 1779, 1749, 1652, 1792, 1783, 2064, 2062, 2205, 2170, 2075, 2161,
2087, 1959, 1990, 1991, 1938, 1926, 2018, 1946, 1921, 2001, 1872, 1715, 1847, 1816, 1693, 1765, 1744, 1672,
1744, 1749, 1733, 1734, 1830, 1740, 1686, 1804, 1875, 2403, 3280, 4095, 3829, 2643, 1472, 1213, 1642, 1922,
1872, 1808, 1857, 1814, 1746, 1882, 1829, 1783, 1935, 1911, 1874, 2064, 2085, 2042, 2139, 2193, 2147, 2191,
2292, 2271, 2250, 2411, 2361, 2506, 2422, 2289, 2433, 2381, 2272, 2291, 2227, 2095, 2026, 2077, 2001, 1942,
1986, 1899, 1819, 1919, 1851, 1718, 1860, 1806, 1806, 1929, 1919, 1851, 1872, 2000, 1921, 1914, 2005, 1952,
1888, 2038, 1993, 1902, 2009, 2013, 1905, 1971, 1921, 1883, 1932, 2005, 1915, 1880, 2054, 2059, 2017, 2129,
2112, 2095, 2197, 2130, 2006, 2082, 2007, 1947, 1984, 2064, 2000, 1951, 2030, 1962, 1945, 2066, 2021, 1907,
2010, 1981, 1914, 1998, 2014, 1906, 1979, 2081, 2093, 2036, 2157, 2112, 2060, 2119, 2034, 1954, 2095, 2007,
1890, 1922, 1952, 1885, 1970, 2069, 2034, 2013, 2043, 1968, 1973, 2346, 3045, 3261, 3824, 3619, 2992, 1808,
1595, 1900, 2373, 2389, 2094, 2185, 2376, 2274, 2394, 2338, 2183, 2302, 2229, 2131, 2220, 2206, 2127, 2097,
2126, 2112, 2147, 2269, 2195, 2199, 2352, 2312, 2206, 2312, 2301, 2224, 2307, 2295, 2206, 2213, 2188, 2066,
1990, 2101, 1999, 1975, 2063, 2019, 1918, 2066, 2034, 1971, 2096, 2102, 2002, 2079, 2128, 2081, 2235, 2238,
2177, 2278, 2224, 2142, 2268, 2260, 2180, 2249, 2253, 2207, 2227, 2249, 2158, 2116, 2185, 2080, 2020, 2119,
2037, 1912, 1986, 1937, 1848, 1922, 1911, 1814, 1821, 1829, 1736, 1798, 1874, 1814, 1705, 1872, 1843, 1761,
# 确保ECG数据足够长,以覆盖update_ecg中的绘制范围
]
app = QApplication(sys.argv)
ex = PacemakerSimulator(ecg_data)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
中间那一段数据很长,是以前做项目时我室友的心电,可以适当删除部分