Using Python for Desktop Notifications

Loading

Python allows you to send desktop notifications across different operating systems. This is useful for alerting users about important events, reminders, or background tasks.


Why Use Desktop Notifications?

Alerts users about important events
Enhances productivity with reminders
Notifies task completion in automation scripts


1. Using plyer for Simple Notifications

The plyer module provides a cross-platform API for notifications.

Installation:

pip install plyer

Example: Sending a Notification

from plyer import notification

notification.notify(
title="Task Completed",
message="Your automated script has finished execution.",
timeout=5 # Notification disappears after 5 seconds
)

Now a notification appears on the desktop!


2. Customizing Notifications

Adding an Icon

notification.notify(
title="Reminder",
message="Time to take a break!",
app_icon="icon.ico", # Path to icon file (Windows: .ico, Linux: .png)
timeout=10
)

Now the notification includes an icon!


3. Using win10toast for Persistent Notifications (Windows Only)

The win10toast module keeps the notification visible until dismissed.

Installation:

pip install win10toast

Example: Windows Toast Notification

from win10toast import ToastNotifier

toaster = ToastNotifier()
toaster.show_toast("Alert", "Your process has started!", duration=10)

Now a Windows notification remains visible until closed!


4. Using notify-send (Linux Only)

On Linux, you can use the notify-send command via subprocess.

Example: Sending a Notification on Linux

import subprocess

subprocess.run(["notify-send", "Linux Alert", "Your script has finished running."])

Now a notification appears on Linux desktops!


5. Scheduling Notifications

Use schedule to send notifications at specific intervals.

Example: Send a Notification Every Hour

import schedule
import time

def send_reminder():
notification.notify(
title="Hydration Reminder",
message="Drink some water!",
timeout=5
)

# Schedule the task
schedule.every().hour.do(send_reminder)

while True:
schedule.run_pending()
time.sleep(60)

Now a reminder appears every hour!

Leave a Reply

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