Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Windows 10 Toast Notifications with Python
We can create toast notifications for Windows 10 events using Python. This is very simple with the win10toast module. If you are familiar with Toast notifications in Android, then understanding toast notifications with Python is straightforward. We can generate notifications whenever an event occurs as a reminder.
Installation
Run the following command in command-line to install the win10toast module ?
pip install win10toast
If the module is successfully installed, you will get output similar to this ?
Collecting win10toast Downloading https://files.pythonhosted.org/packages/d4/ba/95c0ea87d9bcad68b90d8cb130a313b939c88d8338a2fed7c11eaee972fe/win10toast-0.9-py2.py3-none-any.whl Collecting pypiwin32 (from win10toast) Installing collected packages: pypiwin32, win10toast Successfully installed pypiwin32-223 win10toast-0.9
Steps to Create a Toast Notification
Import
ToastNotifierclass from thewin10toastmodule.Instantiate the class.
Invoke the
show_toast()method with desired arguments.The method returns
Trueafter notification duration completes, if successful.
Basic Example
Let's create a simple toast notification ?
# program to generate a simple toast notifier
from win10toast import ToastNotifier
# instantiating the class
notifier = ToastNotifier()
# invoking the show_toast() method with required arguments
notifier.show_toast("Sample Notification",
"You are learning at TutorialsPoint",
duration=10,
icon_path="globe.ico")
When you run this program, a toast notification will appear in the bottom-right corner of your Windows 10 screen.
Method Parameters
The show_toast() method accepts the following parameters ?
title− The notification title (required)msg− The notification message (required)duration− How long to display the notification in seconds (default: 5)icon_path− Path to an .ico file for custom icon (optional)threaded− Whether to run in separate thread (default: False)
Example with Custom Duration
Here's an example with a longer duration ?
from win10toast import ToastNotifier
notifier = ToastNotifier()
# Show notification for 15 seconds
notifier.show_toast("Task Complete",
"Your Python script has finished executing!",
duration=15)
Threaded Notifications
Use threaded notifications to avoid blocking your main program ?
from win10toast import ToastNotifier
import time
notifier = ToastNotifier()
# Non-blocking notification
notifier.show_toast("Background Task",
"Processing in background...",
duration=5,
threaded=True)
# Your main program continues here
print("Main program continues running...")
time.sleep(10)
print("Main program finished")
Common Use Cases
Script completion alerts − Notify when long-running scripts finish
System monitoring − Alert for disk space, CPU usage, etc.
Scheduled reminders − Combine with scheduling libraries
Error notifications − Alert users when exceptions occur
Conclusion
Windows 10 toast notifications with Python are easy to implement using the win10toast module. Use the show_toast() method to display custom notifications with titles, messages, durations, and icons for various system events and alerts.
