level 4
科技小郑
楼主
所有计算机都可以使用这个充电功能,无论是Windows、Linux还是其他装有Python的操作系统。同时,笔记本电脑的电池是最容易损坏的部件,因此保护好电池能够显著延长笔记本电脑的使用寿命。
这个Py文件的充电提醒功能在任何计算机都能使用,前提是操作系统上装有python语言。
需要安装两个第三方库:psutil、pyttsx3,使用cmd安装:pip install psutil,或者其他安装方法安装上着两个库。
功能描述:当电池电量充电30%,语音提醒一次;当电池电量充电80%,语音提醒一次;当电池电量充电100%,语音提醒一次;
import psutil # pip install psutil
import pyttsx3 # pip installl
import time
engine = pyttsx3.init() # 初始化语音引擎
engine.setProperty('volume', 0.65)
engine.setProperty('rate', 120)
battery_info = psutil.sensors_battery() # 获取电池信息
is_charging = battery_info.power_plugged # 判断是否正在充电,这里假设电池的power_plugged属性为True表示正在充电
battery_percent = battery_info.percent # 获取电池电量百分比
fully_charged_text = "电池已充满,请及时断开充电器!" # 定义满电时的文本
battery80_text = "当前电量已到百分之80!"
battery30_text = "当前电量已到百分之30!"
battery100_in = False
battery80_in = False
battery30_in = False
battery80_out = False
battery30_out = False
while True: # 无限循环,持续监控电池状态
battery_info = psutil.sensors_battery() # 更新电池信息
is_charging = battery_info.power_plugged # 更新是否正在充电的状态
battery_percent = battery_info.percent # 更新电池电量百分比
if is_charging: # 如果开始充电
battery80_out = battery30_out = False # 重置所有标志,以便下次充电时重新提醒
if battery_percent == 100 and not battery100_in: # 如果电池电量已满
engine.say(fully_charged_text) # 通过语音引擎播报电池已满的信息
engine.runAndWait() # 立即执行语音播报
battery100_in = True
elif battery_percent == 80 and not battery80_in: # 如果电量降到80%且之前未提醒过
engine.say(battery80_text) # 提醒电量已到80%
engine.runAndWait()
battery80_in = True # 设置已提醒标志
elif battery_percent == 30 and not battery30_in: # 如果电量降到30%且之前未提醒过
engine.say(battery30_text) # 提醒电量已到30%
engine.runAndWait()
battery30_in = True # 设置已提醒标志
else: # 如果停止充电
battery100_in = battery80_in = battery30_in = False # 重置所有标志,以便下次充电时重新提醒
if battery_percent == 80 and not battery80_out: # 如果电量降到80%且之前未提醒过
engine.say(battery80_text) # 提醒电量已到80%
engine.runAndWait()
battery80_out = True # 设置已提醒标志
elif battery_percent == 30 and not battery30_out: # 如果电量降到30%且之前未提醒过
engine.say(battery30_text) # 提醒电量已到30%
engine.runAndWait()
battery30_out = True # 设置已提醒标志
time.sleep(1) # 每次循环休眠1秒,以降低CPU使用率
2024年01月01日 08点01分
1
这个Py文件的充电提醒功能在任何计算机都能使用,前提是操作系统上装有python语言。
需要安装两个第三方库:psutil、pyttsx3,使用cmd安装:pip install psutil,或者其他安装方法安装上着两个库。
功能描述:当电池电量充电30%,语音提醒一次;当电池电量充电80%,语音提醒一次;当电池电量充电100%,语音提醒一次;
import psutil # pip install psutil
import pyttsx3 # pip installl
import time
engine = pyttsx3.init() # 初始化语音引擎
engine.setProperty('volume', 0.65)
engine.setProperty('rate', 120)
battery_info = psutil.sensors_battery() # 获取电池信息
is_charging = battery_info.power_plugged # 判断是否正在充电,这里假设电池的power_plugged属性为True表示正在充电
battery_percent = battery_info.percent # 获取电池电量百分比
fully_charged_text = "电池已充满,请及时断开充电器!" # 定义满电时的文本
battery80_text = "当前电量已到百分之80!"
battery30_text = "当前电量已到百分之30!"
battery100_in = False
battery80_in = False
battery30_in = False
battery80_out = False
battery30_out = False
while True: # 无限循环,持续监控电池状态
battery_info = psutil.sensors_battery() # 更新电池信息
is_charging = battery_info.power_plugged # 更新是否正在充电的状态
battery_percent = battery_info.percent # 更新电池电量百分比
if is_charging: # 如果开始充电
battery80_out = battery30_out = False # 重置所有标志,以便下次充电时重新提醒
if battery_percent == 100 and not battery100_in: # 如果电池电量已满
engine.say(fully_charged_text) # 通过语音引擎播报电池已满的信息
engine.runAndWait() # 立即执行语音播报
battery100_in = True
elif battery_percent == 80 and not battery80_in: # 如果电量降到80%且之前未提醒过
engine.say(battery80_text) # 提醒电量已到80%
engine.runAndWait()
battery80_in = True # 设置已提醒标志
elif battery_percent == 30 and not battery30_in: # 如果电量降到30%且之前未提醒过
engine.say(battery30_text) # 提醒电量已到30%
engine.runAndWait()
battery30_in = True # 设置已提醒标志
else: # 如果停止充电
battery100_in = battery80_in = battery30_in = False # 重置所有标志,以便下次充电时重新提醒
if battery_percent == 80 and not battery80_out: # 如果电量降到80%且之前未提醒过
engine.say(battery80_text) # 提醒电量已到80%
engine.runAndWait()
battery80_out = True # 设置已提醒标志
elif battery_percent == 30 and not battery30_out: # 如果电量降到30%且之前未提醒过
engine.say(battery30_text) # 提醒电量已到30%
engine.runAndWait()
battery30_out = True # 设置已提醒标志
time.sleep(1) # 每次循环休眠1秒,以降低CPU使用率
