Realtime CPU monitor using PyGame


game development

Here’s a realtime CPU monitor using PyGame:

'''
Author: https://github.com/Abdur-rahmaanJ
Instructions: 
    pip install psutil hooman==0.3.6
'''

from hooman import Hooman
import psutil
import pygame

window_width, window_height = 500, 500
hapi = Hooman(window_width, window_height)

bg_col = (255, 255, 255)


loop_var = 0
time_unit = 0
graph_data = []


hapi.stroke_size(3)
hapi.stroke(hapi.color['black'])

while hapi.is_running:
    loop_var += 1
    hapi.background(bg_col)

    if loop_var % 10 == 0:
        time_unit += 1
        graph_data.append([round(time_unit), round(psutil.cpu_percent())])
        #print(graph_data)

    if graph_data:
        range_data = list(zip(*graph_data ))
        max_time =  round(max(range_data[0]))
        max_cycle =  round(max(range_data[1]))
    else:
        max_time = 100
        max_cycle = 100

    hapi.linechart(
        30,
        30,
        400,
        300,
        {
            "data": graph_data,
            "mouse_line": True,
            "range_y": [0, max_cycle],
            "range_x": [0, max_time],
        },
    )

    hapi.event_loop()
    hapi.flip_display()

pygame.quit()


output:

Written by

Abdur-Rahmaan Janhangeer

Chef

Python author of 7+ years having worked for Python companies around the world

Suggested Posts

Display Most Frequent Words Using Pygame

""" Author: Abdur-Rahmaan Janhangeer Github: https://github.com/Abdur-rahmaanJ Instructions: pip...

Read article

How to integrate P5JS with Flask-SocketIO

P5Js is an implementation of processing.org’s library in JavaSript. It can be thought of as a canvas...

Read article

Creating Smooth Mouse Trails in Processing.py using OOP

In creative coding, adding a “trail” effect can transform a simple animation into something dynamic ...

Read article
Free Flask Course