Desktop Battery Percentage Notifier using Python

Hi there, today we will be talking about something which is very interesting as well as simple: Create a desktop notifier using Python and for better understanding we will make a desktop battery percentage notifier using python. A few lines of code help to develop this notifier.

So ,let’s get started right away.

Python Battery Percentage Informer Project

Requirements:  

Basic knowledge of python (Python Introduction – Python Programming) . We can use any ide (for example : https://code.visualstudio.com/download ,https://jupyter.org/ ).

Modules required:  

A. psutil (process and system utilities) : we use the psutil module in to retrieve information about the running processes and system utilization on the device ( information about the CPU, memory, disks etc.) .

B. plyer : It is platform-independent Python library for accessing features of the hardware.

Installation:

First we have to open command prompt in administrator mode. Then, enter command  —

pip install psutil 
pip install plyer

Approach:  

Step 1: Firstly, we have to import the psutil module and time module, and from plyer we will import notification .

import psutil
from plyer import notification 
import time 

Step 2: Secondly, we have to call the notify method of the class. In this case, the parameters of the method is Title (title of our notification), Message (required message of our notification) and timeout (required time for displaying the message or notification , we have set it to 10).

notification.notify (                                                                      title = "....." ,                                                                        message = "..." ,                                                                        timeout = 10) 

Step 3: Finally,  we will add a sleep function to show the notification again after some time .

time.sleep (60*60)

It means ,after every 60 minutes or one hour it will show the “battery percentage” notification.

Source Code:

import psutil
from plyer import notification
import time

while(True):
	battery = psutil.sensors_battery()
	percentage = battery.percent
	
	notification.notify(
		title="Battery Percentage",
		message=str(percentage)+"% Battery remaining",
		timeout=10
	)
	
	time.sleep(60*60)

	continue

An alternative method battery notifier ( using psutil and win10toast ):

Win10toast: This module helps us to send and display notifications on desktop. It is not an in-built module ,we have to install it. Open command prompt in administrator mode and then enter command —

pip install win10toast

Approach:  

1: Firstly, we have to import the psutil module, and from win10toast we will import ToastNotifier .

import psutil
from win10toast import TestNotifier

2: Secondly, create an object to ToastNotifier class

notify = ToastNotifier

3: We will use show_toast function, parameters are title ( title of our notification), message (the actual notification message), icon of the notification and the duration of the notification.

notify.show_toast( )

Source Code :

import psutil                                                                               from win10toast import ToastNotifier                                                            battery = psutil.sensors_battery()                                                       percentage = battery.percent                                                              notify = ToastNotifier()                                                 notify.show_toast("Battery Percentage", str(percentage), icon_path =None, duration =20)

Note :  

If we write icon_path = None , then by default the icon will be the logo of python.

Python Battery Notifer

One Comment

Comments are closed.