Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python获取电池百分比?_Python_Python 3.x_Battery - Fatal编程技术网

如何使用python获取电池百分比?

如何使用python获取电池百分比?,python,python-3.x,battery,Python,Python 3.x,Battery,如何使用python获取电池百分比? 可能一些win32api函数会有所帮助。试试以下方法: import psutil battery = psutil.sensors_battery() plugged = battery.power_plugged percent = str(battery.percent) plugged = "Plugged In" if plugged else "Not Plugged In" print(percent+'

如何使用python获取电池百分比?
可能一些win32api函数会有所帮助。

试试以下方法:

import psutil
battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = str(battery.percent)
plugged = "Plugged In" if plugged else "Not Plugged In"
print(percent+'% | '+plugged)

图书馆链接是。检查一下

当对象具有这些属性时,为什么要解析字符串?你只需要
电池。百分比
电池。电源插头
。好的。我将很快更改它。传感器电池()有时返回空值?还有别的选择吗?