Friday, April 29, 2022

Display CPU and Virtual Memory in Realtime with PyQt6 and Matplotlib

 This demo program shows how to display in real time the CPU usage and Virtual Memory Usage in % using a donut shape graph from matplotlib.

The output:



The Code:

  1
 
159
import sys
import psutil
import os
from PyQt6.QtWidgets import QApplication, QWidget, QLabel
from PyQt6.QtGui import QFont
from PyQt6.QtCore import QTimer, QTime, Qt

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from matplotlib.patches import Circle

i = 5
d = 95
class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        super(MplCanvas, self).__init__(Figure(figsize=(width, height), dpi=dpi))
        self.axes = self.figure.add_subplot(111)
        self.setParent(parent)


class MplWidget(QWidget):
    def __init__(self, parent=None):
        global i
        global d
        QWidget.__init__(self, parent)

        self.canvas = MplCanvas(self)
        self.canvas1 = MplCanvas(self)
        d = 100 - i
        labels = ["a", "b"]
        data = [d, i]
        
        #colors = ["#f2f2f2", "#ff9999"]
        colors = ["#f2f2f2", "blue"]

        self.canvas.axes.pie(
                data,
                colors=colors,
                startangle=90,
               pctdistance=0.85
        )
        
        self.canvas1.axes.pie(
                data,
                colors=colors,
                startangle=90,
               pctdistance=0.85
        )

        centre_circle = Circle((0, 0), 0.70, fc="white")
        self.canvas.axes.add_artist(centre_circle)
        self.canvas.axes.axis("equal")
        self.canvas.figure.tight_layout()
        self.canvas.setGeometry(75,75,100,100)
        self.canvas.draw()
        centre_circle1 = Circle((0, 0), 0.70, fc="white")
        self.canvas1.axes.add_artist(centre_circle1)
        self.canvas1.axes.axis("equal")
        self.canvas1.figure.tight_layout()
        self.canvas1.setGeometry(200,75,100,100)
        self.canvas1.draw()
        
        fnt = QFont('Open Sans', 11, QFont.Weight.Bold)
        fnt1 = QFont('Open Sans', 9)
        self.lbl = QLabel(self)
        self.lbl.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.lbl.setFont(fnt)
        
        self.lbl.setGeometry(78,110,100, 30)
        
        self.lbl1 = QLabel(self)
        self.lbl1.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.lbl1.setFont(fnt1)
        
        self.lbl1.setGeometry(78,50,100, 30)
        self.lbl1.setText('CPU Usage')
        
        self.lbl2 = QLabel(self)
        self.lbl2.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.lbl2.setFont(fnt)
        
        self.lbl2.setGeometry(200,110,100, 30)
        
        self.lbl3 = QLabel(self)
        self.lbl3.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.lbl3.setFont(fnt1)
        
        self.lbl3.setGeometry(200,50,100, 30)
        self.lbl3.setText('Virtual Mem Usage')
        
        
        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(10000) # update every second
        self.setGeometry(50,75,400,250)
        self.setWindowTitle('Post 29')
        self.showTime()
    def showTime(self):
        global i
        global d
        #currentTime = QTime.currentTime()
        i = psutil.cpu_percent()
        #displayTxt = currentTime.toString('hh:mm:ss')
        self.lbl.setText(str(i) + '%')
        
            
        d = 100 - i
        labels = ["a", "b"]
        data = [d, i]
        
        colors = ["#f2f2f2", "blue"]

        self.canvas.axes.pie(
                data,
                colors=colors,
                startangle=90,
               pctdistance=1
        )
        colors = ["#f2f2f2", "green"]
        centre_circle = Circle((0, 0), 0.85, fc="white")
        self.canvas.axes.add_artist(centre_circle)
        self.canvas.axes.axis("equal")
        self.canvas.figure.tight_layout()
        self.canvas.setGeometry(75,75,100,100)
        self.canvas.draw()
        i = psutil.virtual_memory().available * 100 / psutil.virtual_memory().total
        self.lbl2.setText(str(round(i,2)) + '%')
        d = 100 - i
        data = [d, i]
        self.canvas1.axes.pie(
                data,
                colors=colors,
                startangle=90,
               pctdistance=1
        )
        
        centre_circle1 = Circle((0, 0), 0.85, fc="white")
        self.canvas1.axes.add_artist(centre_circle1)
        self.canvas1.axes.axis("equal")
        self.canvas1.figure.tight_layout()
        self.canvas1.setGeometry(200,75,100,100)
        self.canvas1.draw()
            #self.update
       
                    
        i = i + 5
        

def main():
    app = QApplication(sys.argv)

    widget = MplWidget()
    widget.show()

    sys.exit(app.exec())


if __name__ == "__main__":
    main()


No comments:

Post a Comment