Today we will learn how to send desktop notifications with Python. For this we will just need a computer with Python installed.
Packages and Setup
You’ll need Plyer’s notification for this project:
# In CMD or terminal
pip install plyer
# In project
from plyer import notification
You can send notifications by calling the notify()
function from notification.
from plyer import notification
notification.notify(
title = 'testing',
message = 'message',
app_icon = None,
timeout = 10,
)
It takes four parameters:
title
The large header text at the top of a notification.
message
The longer, smaller text where you put more detailed information.
app_icon
The image that appears next to the title and message.
timeout
How long the message should show on screen.
If we run it right now, our notification should show up!
Figure 1: Notification!
đź’ˇ Speed up your blog creation with DifferAI.
Available for free exclusively on the free and open blogging platform, Differ.
Improving the design
Let’s add an image. All we have to do for this part is specify the file path of the image!
Note: You have to use a _.ico_
file.
app_icon = "image.ico"
Figure 2: Notification with image!
Delay
You can enforce a delay by using Python’s time.sleep()
:
from plyer import notification
import time
while True:
time.sleep(30)
notification.notify(
title = 'testing',
message = 'message',
app_icon = "image.ico",
timeout = 10,
)
And it shows up every 30 seconds!
Figure 3: Notification with delay!
24/7 365
You can run this file forever with pythonw
.
cd
into the directory your .py
file is, and then execute the command below
pythonw filename.py
It should execute without any errors.
We can confirm it works by going to our task manager and see the active tasks.
Figure 4: Python file running in background.
To kill the process, end the task.
If that doesn’t work, and you only have one pythonw
task running, you can use this command.
taskkill /IM pythonw.exe /F
Conclusion
I hope you enjoyed reading this, and that you find a good use for this tool! If you have any questions, suggestions, or general feedback, put it in the comments! Thanks!