Javatpoint Logo
Javatpoint Logo

Desktop Battery Notifier using Python

In this tutorial, we will write the program to create desktop/laptop battery notifier and send the battery percentage notification using Python programming language. As a laptop user, we must aware of the battery percentage of the laptop. What if we get a program that reminds us the battery of the laptop using notification?

We will use the Psutil library. So let's have a brief introduction of this library.

Psutil Library in Python

Psutil library is a cross-platform library used to get the information on running processes and system utilization such as CPU, memory, disks, networks, sensors in Python.

To work with this library, we need to install the two modules - Psutil and plyer. Type the following command in the terminal to install these libraries.

To install the pyler module, type the following command in the terminal.

The psutil library provides the sensor_battery() method which returns the named tuple which have following values. It returns the None, if the battery is not installed.

Percent - It gives the power remaining in the working laptop. Let's see the following example -

Example -

Output:

57.17304531580183

We can also check if the charger is not plugged or not, we will the following method.

The above method will return either True or False. It returns True if power is plugged in, False, it isn't charging.

Now we will write the program to create the Desktop notifier using the Python. Let's understand the following program.

Desktop Notifier Program using Python

Example-

Output -

Explanation -

In the above code, we import the psutil and pyler modules. Then we create the check_battery_status() method that takes two arguments - battery and plugged which are nothing but a battery status and status of system plugged in or not. First we check if the system is plugged, if it returns true then we get the percent of the battery at the time of charging, if it is above than 80, send notification with the title and message. Then, we check if the battery percent is equal to hundred, send the message with title. It checks every possibility when we can inform user to charge their laptop.

As we can see in the above code, we can easily remind about the battery percentage. When you run the above program, you will see the desktop notification based on your laptop/desktop battery percentage.

Conclusion

This tutorial included the desktop notification program using Python. We explained the how we can show the notification with message using the psutil and pyler library. You can copy the above code and paste into your system and can check the output.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA