Home > Article > Backend Development > Python script to display laptop battery percentage
Monitoring your laptop's battery percentage is essential to keep track of its power levels and ensure uninterrupted use. Whether you work on the go or rely on your laptop for long periods of time, using a Python script to display battery percentage can be extremely useful. In this blog post, we’ll explore how to create a Python script that displays your laptop’s battery percentage.
In the following sections, we discuss the prerequisites for running the script, explore how to retrieve battery information, and demonstrate how to display battery percentage in a graphical user interface (GUI).
Before you begin, you need to meet some prerequisites to successfully run the Python script that displays your laptop's battery percentage. This is what you need−
Python − Make sure Python is installed on your system. You can download the latest version of Python from the official Python website (https://www.python.org/) and follow the installation instructions specific to your operating system.
Operating System Compatibility − Please keep in mind that the method of accessing battery information may vary depending on the operating system you are using. The scripts provided in this article are compatible with Windows, macOS, and Linux-based systems, but may require some adjustments for specific operating system versions or distributions.
Required Python libraries − We will use the psutil library to retrieve battery information. Make sure psutil is installed on your system. You can install using pip (Python package installer) by running the following command in a terminal or command prompt:
pip install psutil
After you have installed Python and met the necessary prerequisites, you can proceed to create a Python script.
To display laptop battery percentage using Python, we will use the psutil library, which provides an interface to retrieve system information, including battery-related data. Here's how to create a Python script −
Import necessary modules−
import psutil
Retrieve battery information−
battery = psutil.sensors_battery()
Check if the battery is present−
if battery is None: print("No battery found.") exit()
Get battery percentage−
percentage = battery.percent
Display battery percentage−
print(f"Battery Percentage: {percentage}%") Run the script: if __name__ == "__main__": main()
The complete Python script to display laptop battery percentage should look like this −
import psutil def main(): battery = psutil.sensors_battery() if battery is None: print("No battery found.") exit() percentage = battery.percent print(f"Battery Percentage: {percentage}%") if __name__ == "__main__": main()
Save the script with a meaningful name, such as "battery_percentage.py". Now when you run this script it will display the current battery percentage of your laptop.
Note− Please keep in mind that the script may provide different results depending on the battery status of your laptop. Make sure your laptop is not connected to a power source to get accurate readings.
In the next section, we'll discuss how to run the script and explore other customization options.
To run a Python script that displays laptop battery percentage, follow these steps -
Open a text editor and create a new file.
Copy the Python script provided in the previous section and paste it into the file.
使用有意义的名称保存文件,例如“battery_percentage.py”。
现在,您可以使用以下方法之一运行脚本 −
打开终端或命令提示符,导航到保存脚本的目录,然后运行以下命令−
python battery_percentage.py
如果您使用的是 PyCharm、Visual Studio Code 或 IDLE 等 IDE,则可以打开脚本文件并从 IDE 中执行它。在 IDE 的菜单或工具栏中查找“运行”或“执行”选项。
运行脚本后,您应该会看到笔记本电脑的电池百分比显示在控制台或输出窗口中。
您可以自定义脚本以增强其功能并使其提供更多信息。这里有一些想法 -
添加时间戳 − 显示当前日期和时间以及电池百分比,以跟踪一段时间内的变化。
设置阈值 − 定义可接受的最低电池百分比,并在电池电量低于该阈值时显示警告消息。
创建图形用户界面 (GUI) − 不要在控制台中显示电池百分比,而是使用 Tkinter 或 PyQt 等库构建 GUI 以在窗口中显示信息。
在本教程中,我们学习了如何创建 Python 脚本来显示笔记本电脑电池百分比。我们使用 psutil 库检索系统信息并使用 psutil.sensors_battery() 函数访问电池百分比。通过将此功能合并到脚本中,我们可以方便地监控笔记本电脑的电池状态。
我们首先导入必要的模块并定义一个函数来获取电池百分比。然后我们实现了一个循环来在控制台中持续显示电池百分比。此外,我们还讨论了如何运行该脚本并提供了自定义选项来扩展其功能。
The above is the detailed content of Python script to display laptop battery percentage. For more information, please follow other related articles on the PHP Chinese website!