DIY Desktop Notifications with Python

Creating a python script for a “do it yourself” reminder is very easy! The player library is all we need

First let’s install the library:

pip install plyer
pip install plyer
pip install plyer

Enter fullscreen mode Exit fullscreen mode

Before moving on to our reminder script, let’s take a look at the notification module that will allow us to generate a notification.

The import of the notification module is done like this:

from plyer import notification
from plyer import notification
from plyer import notification

Enter fullscreen mode Exit fullscreen mode

The function to use will be notification.notify, its main parameters are (full docs here):

title (str) - Title of the notification
message (str) - Message contained within the notification
app_name (str) - Name of the application
app_icon (str) - Icon to display next to the notification message
timeout (int) - Time to display the notification in seconds (default 10)
ticker (str) - Text to display in the notification bar
title (str) - Title of the notification 
message (str) - Message contained within the notification 
app_name (str) - Name of the application 
app_icon (str) - Icon to display next to the notification message 
timeout (int) - Time to display the notification in seconds (default 10) 
ticker (str) - Text to display in the notification bar
title (str) - Title of the notification message (str) - Message contained within the notification app_name (str) - Name of the application app_icon (str) - Icon to display next to the notification message timeout (int) - Time to display the notification in seconds (default 10) ticker (str) - Text to display in the notification bar

Enter fullscreen mode Exit fullscreen mode


We are ready

Now that everything is configured and described, we are ready for our script.

The script below will display a notification with the title
“⏰ REMINDER” and the message “Have a break ”.

The notification will appear for 5 seconds ( timeout=5 ) every 15 seconds ( time.sleep(15) ).

The notification will have as its icon the file “icon.ico” contained within the folder where we decide to save our script.

Please note: the “app_icon” parameter only accepts .ico type files

import time
import pathlib
from plyer import notification
curr_path = pathlib.Path(__file__).resolve().parent
notification_message= '''
Have a break
'''
if __name__ == "__main__" :
while ( True ):
notification.notify(
title= "⏰ REMINDER" ,
message=notification_message,
timeout= 5 ,
app_name= "My Notification" ,
app_icon= str (curr_path)+ "\\icon.ico" ,
)
time.sleep( 15 )
import time 
import pathlib 
from plyer import notification 

curr_path = pathlib.Path(__file__).resolve().parent 

notification_message= ''' 
Have a break  
''' 

if __name__ == "__main__" : 
    while ( True ): 

        notification.notify( 
            title= "⏰ REMINDER" , 
            message=notification_message, 
            timeout= 5 , 
            app_name= "My Notification" , 
            app_icon= str (curr_path)+ "\\icon.ico" , 
        ) 

        time.sleep( 15 )
import time import pathlib from plyer import notification curr_path = pathlib.Path(__file__).resolve().parent notification_message= ''' Have a break ''' if __name__ == "__main__" : while ( True ): notification.notify( title= "⏰ REMINDER" , message=notification_message, timeout= 5 , app_name= "My Notification" , app_icon= str (curr_path)+ "\\icon.ico" , ) time.sleep( 15 )

Enter fullscreen mode Exit fullscreen mode

Here is the final result:


Follow me #techelopment

Medium: @techelopment
Dev.to: Techelopment
facebook: Techelopment
instagram: @techelopment
X: techelopment
telegram: @techelopment_channel
youtube: @techelopment
whatsapp: Techelopment

原文链接:DIY Desktop Notifications with Python

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
Whatever I believed, I did; and whatever I did, I did with my whole heart and mind.
凡是我相信的,我都做了;凡是我做了的事,都是全身心地投入去做的
评论 抢沙发

请登录后发表评论

    暂无评论内容