#!/usr/bin/env python3
import subprocess
import sys
arg = sys.argv[1]
# get the data on screens and current brightness, parsed from xrandr --verbose
current = [l.split() for l in subprocess.check_output(["xrandr", "--verbose"]).decode("utf-8").splitlines()]
# find the name(s) of the screen(s)
screens = [l[l.index("connected")-1] for l in current if "connected" in l]
# find the current brightness
currset = (round(float([l for l in current if "Brightness:" in l][0][1])*10))/10
# create a range of brightness settings (0.1 to 1.0)
sets = [n/10 for n in list(range(11))][1:]
# get the current brightness -step
step = len([n for n in sets if currset >= n])
if arg == "up":
if currset < 1.0:
# calculte the first value higher than the current brightness (rounded on 0.1)
nextbright = (step+1)/10
if arg == "down":
if currset > 0.1:
# calculte the first value lower than the current brightness (rounded on 0.1)
nextbright = (step-1)/10
try:
for scr in screens:
# set the new brightness
subprocess.Popen(["xrandr", "--output", scr, "--brightness", str(nextbright)])
except NameError:
pass
使用下面的脚本,您可以在任何“服从”的系统上分 9 步将屏幕亮度从 设置
0.1
为。1.0
xrandr
只需使用参数“向上”或“向下”运行它即可增加/减少当前亮度一步。
剧本
如何使用
set_brightness.py
通过以下命令测试运行它:
和
如果一切正常,将这两个命令添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并将上面的两个命令添加到两个不同的快捷键。
解释
代码的解释几乎在脚本中:)
笔记
事实上,脚本为“主”屏幕和可能的附加屏幕设置了相同的亮度。