如题所示,因为本人会经常开着电脑,不关机。就写了一个通过判断显示状态来自动切换节能和卓越性能的软件。
[Python] 纯文本查看 复制代码 import schedule
import time
import subprocess
import wmi
print("欢迎使用电源状态自动切换软件")
def list_all_devices():
c = wmi.WMI()
devices = c.Win32_PnPEntity()
for device in devices:
print(f"设备名称: {device.Name}, 当前状态: {device.Status}")
# 调用函数,输出所有设备的名称及状态
#list_all_devices()
def check_device_exists(device_name):
c = wmi.WMI()
devices = c.Win32_PnPEntity()
flag = 0
for device in devices:
if device.Name == device_name:
print(f"设备名称: {device_name}, 当前状态: {device.Status}")
flag = 1
break
if flag == 0:
print(f"设备名称: {device_name}, 当前状态: 未找到")
return False
else:
return True
def Find_batterystate():
p = subprocess.Popen('powercfg /getactivescheme',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding='gb2312'
)
# 输出stdout
#print(p.communicate()[0])
output=p.communicate()[0]
print(output)
power_state=4
if(output=="电源方案 GUID: a1841308-3541-4fab-bc81-f71556f20b4a (节能)"):
power_state=0
elif(output=="电源方案 GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (平衡)"):
power_state=1
elif(output=="电源方案 GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (高性能)"):
power_state=2
elif(output=="电源方案 GUID: d53ad8e4-e96a-4ce8-a848-50cd120d1b80 (卓越性能)"):
power_state=3
else:
power_state=4
print(power_state)
return(power_state)
def change_low():
subprocess.call('PowerCfg.exe /setactive a1841308-3541-4fab-bc81-f71556f20b4a', shell=True)
def change_balance():
subprocess.call('PowerCfg.exe /setactive 381b4222-f694-41f0-9685-ff5bb260df2e', shell=True)
def change_high():
subprocess.call('PowerCfg.exe /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', shell=True)
def change_highhigh():
subprocess.call('PowerCfg.exe /setactive d53ad8e4-e96a-4ce8-a848-50cd120d1b80', shell=True)
def run():
power_state=Find_batterystate()
result1 = check_device_exists("ASUS VG27AQL1A(DisplayPort)")
result2 = check_device_exists("Generic Monitor (ORAYDISPLAY)")
#print(power_state)
if(power_state==4):
print("erreor!!!")
return
if (result1+result2 > 0):
if(power_state!=3):
change_highhigh()
print("电源模式已切换成卓越模式")
else:
if(power_state!=0):
change_low()
print("电源模式已切换成省电模式")
schedule.every(0.1).minutes.do(run)
while True:
schedule.run_pending()
|