Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3546

Raspberry Pi OS • Trixi RAM Memory Usage Graph Widget

$
0
0
The current trixi release for the RPi-5 does not appear to offer a RAM Memory Usage Graph Widget, so I enlisted the help of ChatGPT5 to create one. While it's not status-bar dockable, it's useful nonetheless.

Code:

#!/usr/bin/env python3# LGD: Memory Bar Graph Widget (fixed bar width, right-click to close)import sys, collections, psutilfrom PySide6 import QtCore, QtGui, QtWidgetsBAR_WIDTH = 3          # fixed bar width in pixelsHISTORY = 300          # max samples stored (only the rightmost shown)INTERVAL_MS = 1000     # update every 1 secondclass MemGraph(QtWidgets.QWidget):    def __init__(self):        super().__init__()        self.setWindowTitle("Memory Usage")        self.resize(300, 120)        self.history = collections.deque([0] * HISTORY, maxlen=HISTORY)        self.timer = QtCore.QTimer(self)        self.timer.timeout.connect(self.update_mem)        self.timer.start(INTERVAL_MS)        self.update_mem()    # --- user interaction ---    def mousePressEvent(self, event):        if event.button() == QtCore.Qt.MouseButton.RightButton:            QtWidgets.QApplication.quit()    # -------------------------    def update_mem(self):        vm = psutil.virtual_memory()        self.history.append(vm.percent)        self.setWindowTitle(f"Memory: {vm.percent:.0f}% ({vm.used/2**30:.2f}/{vm.total/2**30:.2f} GiB)")        self.update()    def paintEvent(self, event):        p = QtGui.QPainter(self)        p.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing, False)        rect = self.rect()        w, h = rect.width(), rect.height()        # Background        p.fillRect(rect, QtGui.QColor(0, 0, 0, 230))        # Grid lines + labels        p.setFont(QtGui.QFont("Sans", 7))        grid_pen = QtGui.QPen(QtGui.QColor(90, 90, 90))        text_pen = QtGui.QPen(QtGui.QColor(170, 170, 170))        for y_pct in (0, 25, 50, 75, 100):            y = int(h * (1 - y_pct / 100))            p.setPen(grid_pen)            p.drawLine(0, y, w, y)            p.setPen(text_pen)            p.drawText(4, max(10, y - 2), f"{y_pct}%")        # Baseline        p.setPen(QtGui.QPen(QtCore.Qt.GlobalColor.white))        p.drawLine(0, h - 1, w, h - 1)        # Draw bars (newest right)        n_visible = w // BAR_WIDTH        visible = list(self.history)[-n_visible:] if len(self.history) > n_visible else list(self.history)        for i, val in enumerate(visible):            x = w - (len(visible) - i) * BAR_WIDTH            bar_height = int(h * val / 100.0)            y = h - bar_height            # Color logic            if val >= 90:                color = QtGui.QColor(230, 40, 40)            elif val >= 75:                color = QtGui.QColor(255, 180, 0)            else:                color = QtGui.QColor(50, 220, 50)            color.setAlpha(220)            p.fillRect(x, y, BAR_WIDTH - 1, bar_height, color)        # Border        p.setPen(QtGui.QPen(QtCore.Qt.GlobalColor.lightGray))        p.drawRect(rect.adjusted(0, 0, -1, -1))def main():    app = QtWidgets.QApplication(sys.argv)    win = MemGraph()    win.show()    sys.exit(app.exec())if __name__ == "__main__":    main()

Write the code to memgraph-widget.py, and make it executable: chmod 755 ./memgraph-widget.py.

You can run it asynchronously: ./memgraph-widget.py &
Simply right-click it to kill it.

If you like these little scripts, you may find something useful here: https://github.com/LDighera/Linux-tools

Best regards,
Larry

Statistics: Posted by LDighera — Tue Oct 28, 2025 6:06 pm



Viewing all articles
Browse latest Browse all 3546

Trending Articles