-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUmonitor.py
61 lines (44 loc) · 1.62 KB
/
CPUmonitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import psutil
from PySide6 import QtCore, QtWidgets, QtGui
import pyqtgraph
import sys
import time
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QVBoxLayout(self)
self.setWindowTitle("CPU Monitor")
self.text = QtWidgets.QLabel("Starting...", alignment=QtCore.Qt.AlignCenter)
self.layout.addWidget(self.text)
self.plotWidget = pyqtgraph.PlotWidget()
self.plotWidget.setYRange(0, 100)
self.plotWidget.setTitle("System Monitor: CPU Usage")
self.plotWidget.setLabel("left", "CPU Usage %")
self.plotWidget.setLabel("bottom", "Time (seconds)")
self.layout.addWidget(self.plotWidget)
self.data = []
self.max = 100
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.runCPU)
self.timer.start(1000)
self.plotWidget.setBackground((240, 240, 240))
@QtCore.Slot()
def runCPU(self):
cpu_usage = psutil.cpu_percent(interval=None)
self.data.append(cpu_usage)
if len(self.data) > self.max:
self.data.pop(0)
if cpu_usage < 50:
self.curve = self.plotWidget.plot(pen="g")
elif cpu_usage < 80:
self.curve = self.plotWidget.plot(pen="o")
else:
self.curve = self.plotWidget.plot(pen="r")
self.curve.setData(self.data)
self.text.setText(f"CPU usage: {cpu_usage}%")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Widget()
widget.resize(600, 400)
widget.show()
sys.exit(app.exec())