As a laptop user, you must take caution about your battery percentage as the battery is also the most important component, that’s why today we will see Desktop Battery Notifier using Python.
So, what if your laptop reminds you about the battery percentage using notification. Yes, you read it correctly using a desktop notifier.
In this article, we are going to see the Psutil library in Python using which we can check battery percentage and send the battery percentage desktop notification using another module Plyer.
Psutil Library in Python
Psutil in python is a cross-platform library for retrieving information on running processes and system utilization(CPU, memory, disks, networks, sensors) in Python.
You will need to install two modules one is ‘psutil’ and the other one is ‘plyer’ which will be used to get the notification.
To install ‘psutil’ in your pip. Here’s the command:
pip install psutil
The main function from the psutil module is psutil.sensors_battery() which returns a named tuple consisting of the following values. If no battery is installed or metrics can’t be determined None is returned in Desktop Battery Notifier using Python.
percent: It gives the Power left in your laptop in percentage.
>>>battery.percent
Output:
52 #giving battery percentage
Now, if we want to check if the charger is plugged or not, we will have to use:
>>>power_plugged
This will return either True or False. True if power is plugged in, False if it isn’t charging. Here, in my case it is not plugged hence the output will be:
False
Now, install ‘plyer’ in your pip. Here’s the simple command:
pip install plyer
As we have the package, we’re ready to import it into our python script.
from plyer import notification
Now, let’s specify the parameters. Let’s define the title and message.
title = ‘Battery Reminder’
message = ‘Battery is 52’
Let’s look at what the parameters mean:
title: Title of the notification
description: Message of the notification
timeout: duration to display the message say 10secs as default time used.
Now, let’s pass the parameters using the notify method.
notification.notify( “title”, “message”, timeout)
For eg I’m passing:
the title is ‘Battery Reminder’
message as ‘Battery is 52’
and timeout as 2 secs
That’s it!! We are done and now we are using these functions in our code. So, here goes the complete code for Desktop Battery Notifier using Python.
Desktop Battery Notifier using Python: Complete Code
#import modules from plyer import notification import psutil #returns a tuple battery = psutil.sensors_battery() plugged = battery.power_plugged #description of code if __name__=="__main__": if plugged: percent = battery.percent if percent <= 80: notification.notify( #title of notification title = "Plugged In", #message of notification message=" For better battery life, charge upto 80%" , # displaying time timeout=2 ) elif percent == 100: notification.notify( title = "Plugged In", message=" Please plugged out the charger. Battery is charged" , timeout=2 ) else : notification.notify( title = "Plugged In", message=" Remove the charger please. For better battery life charge up to 80%" , timeout=2 ) else: percent = battery.percent if percent <=20: notification.notify( title = "Battery Reminder", message="Your battery is running low. You might want to plug in your PC " , timeout=2 ) elif percent <=50: notification.notify( title = "Battery Reminder", message=f" Battery is {percent}." , timeout=2 ) elif percent == 100: notification.notify( title = "Battery Reminder", message="Fully charged" , timeout=2 ) else: notification.notify( title = "Battery Reminder", message=f"Battery is {percent}" , timeout=2 )
Now, you know how easy it is to get reminded about your battery percentage. But we will make this more interesting with the help of the output.
Output:
So, here is our desktop notification. Simple isn’t it?? This is how we have successfully done with the ‘Desktop Battery Notifier using Python’. I hope the ‘Psutil’ library is clear to you and don’t forget to try this code once!!
You can play around with the library, explore more features and even customize it further.
Thank You Pythoners!!!
- Games in Python | Assignment Expert
- Creating a Pong Game using Python TurtleIntroduction Today, we are going to create a Pong Game using Python Turtle. Pong is a well-known computer game that is similar to table tennis. The two players in this game control the two paddles on either side of the game window. To hit the moving ball, they move the paddles up and down. A…
- Balloon Shooter Game using Python PyGameWe all know the importance of the pygame library in terms of game development. We know that Pygame is a collection of Python modules for making video games. It is a collection of computer graphics and sound libraries for the Python programming language. Pygame is well suited to the development of client-side applications that may…
- Complete PyGame Tutorial and ProjectsWithout a doubt, we can say that Python is a multi-purpose language. Ranging from basic programming to its application in Data Science, from Machine Learning to Artificial Intelligence, and also in Developing Games. In simple words, it’s the language that is incomparable.With knowing the advantages of Python, today, in this article of Complete PyGame Tutorial…
- Flappy Bird In Python Pygame with source codeOVERVIEW OF THE PROJECT Hi there, welcome to the copyassignment hope you all are doing wonderful. Today, we are here to make a very interesting game Flappy Bird In Python Pygame. Hope you are familiar with this entertaining and very fascinating game. We would see how we can code the game Flappy bird from Scratch….
- Complete Racing Game In Python Using PyGameSo far we have learned a lot but there is not anything to end our game and now we are now going to see part 2 of our project. So, in this section, we will Create a Complete Racing Game In Python Using PyGame. So that we can use it to quit our game. Detecting…
- Complete Game In PyGame and PythonNow, it’s time to create our fully featured Complete Game In PyGame and Python with all the knowledge that we have gained from this series of articles. If you have seen the overview of our game, then maybe you have already made it. If yes then congrats, or you can follow along. To create this…
- Python Complete Setup for PygameNow, it’s time to install the tools that we will use to write programs. So, we will be learning Python Complete Setup for Pygame in this article. Let’s start. 1. Installing Python first. First, we need to go to the official site of python: https://www.python.org/ Now we need to go to the downloads page of…
- Overview of our First Game ProjectSo, congratulations, after learning all the basic and essential concepts of our pygame series, you are finally here to get the Overview of our First Game Project In this article, we will get to know about what game we are gonna make in the next article. But before that, in this article, we will try…
- Car Race Game in PyGame Python: From ScratchCheck the video below to know about our Car Race Game in PyGame and Python we are going to make. If you don’t know Pygame, then you should not worry, we will guide you from scratch. You just need some basic Python skills, other than Python, we will guide you with everything. Introduction to Car…
- Add Background Music and text to PyGameSo, far we have learned how to add images and mechanics to our game. In this article, we will be learning to add Background Music and text to PyGame. So, do the following changes in the final code that we got in our previous article after adding movement. Adding text to PyGame window: Now, adding…
- Moving an object in PyGame|PythonNow as we have learned how to add images, we can finally bring the mechanics/movement to our game elements using pygame. So, the aim of this article is to make you learn how to make PyGame elements moving or Moving an object in PyGame. All in all, we will learn how to move an image…