import wmi
c = wmi.WMI(namespace='root\WMI')
a = c.WmiMonitorBrightnessMethods()[0]
a.WmiSetBrightness(Brightness=80, Timeout=500) # Brightness:亮度
使用wmi实现了亮度调节,但部分电脑可能会出现错误。
安装wmi
pip install wmi
如果嫌下载太慢或者下载出现问题,可以使用国内的pypi网站下载(如:清华大学)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wmi
以下是部分电脑会出现的错误,如果出现其他错误,请参考Microsoft WMI 错误常量:
此方法简单明了,且不会发生错误,推荐使用本方法
import screen_brightness_control as sbc
sbc.set_brightness(0)
安装screen_brightness_control
pip install screen-brightness-control
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple screen-brightness-control
import screen_brightness_control as sbc
import time
while True:
sbc.set_brightness(0)
time.sleep(1)
sbc.set_brightness(100)
如果要边执行别的代码边闪烁,可以使用多线程:
import screen_brightness_control as sbc
import time
import threading
def sb(): # 定义线程执行的函数
while True:
sbc.set_brightness(0)
time.sleep(1)
sbc.set_brightness(100)
threading.Thread(target=sb).run() # target:指定的函数, run():运行线程