1 min read

Python Desktop Notifier

This guide demonstrates how to create desktop notifications using the plyer library. plyer provides a platform-independent API to access features commonly found on various platforms, including notifications.

Modules Used:

  • plyer: To send desktop notifications.
  • time: To handle delays (for periodic notifications).

Installation

pip install plyer

The Code

Save this as notifier.py. This script sends a "Take a Break" notification.

import time
from plyer import notification

def send_notification(title, message):
    notification.notify(
        title=title,
        message=message,
        app_icon=None,  # Path to an .ico file (Windows) or .png (Linux)
        timeout=10,     # Notification stays for 10 seconds
    )

if __name__ == "__main__":
    print("Notifier started. You will be notified in 5 seconds...")

    # Wait for 5 seconds (simulate a timer)
    time.sleep(5)

    send_notification(
        title="Take a Break!",
        message="You've been working hard. Time to stretch your legs."
    )
    print("Notification sent.")

Periodic Notifications

You can easily wrap this in a loop to create a recurring reminder.

import time
from plyer import notification

if __name__ == "__main__":
    while True:
        notification.notify(
            title="Hydration Check",
            message="Drink some water!",
            timeout=10
        )
        # Wait for 1 hour (60 minutes * 60 seconds)
        time.sleep(60 * 60)

Usage

python notifier.py

programming/python/python