How to Build a Desktop Notifier Using Python

How to Build a Desktop Notifier Using Python

In this tutorial, we will be building a desktop notifier app using Python which will notify the user about the CPU usage and ram usage every 10 minutes but can be modified to notify at different intervals

Now to install the above libraries run the commands below.

pip3 install psutil

pip3 install plyer

Now create a file called notify.py and import the following modules as shown below.

import psutil
from plyer import notification
import time

Now let’s create a function to get cpu and ram data usage and then notify the user as shown below.

class DesktopNotifyier:
    def __init__(self, interval=10):
        self.interval = interval

    def system_monitor(self):
        while True:
            cpu_usage = psutil.cpu_percent(interval=1)
            ram_usage = psutil.virtual_memory().percent

            message = f"CPU Usage: {cpu_usage}%\nRAM Usage: {ram_usage}%"

            notification.notify(
                title="System Usage",
                message=message,
                timeout=10
            )

            time.sleep(self.interval)

if __name__ == "__main__":
    monitor = DesktopNotifyier(interval=10)
    monitor.system_monitor()

Run the code above and here is our notification as displayed on our desktop below.

There you have it, Thanks for reading. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *