如何使用Python控制笔记本电脑屏幕亮度?
为了控制屏幕的亮度,我们使用了screen-brightness-control库。屏幕亮度控制库只有一个类,功能很少。下面提到了最有用的功能:
- get_brightness()
- 设置亮度()
- 淡出亮度()
- list_monitors()
安装:我们可以通过运行以下 pip 命令来安装包:
pip install screen-brightness-control
示例 1:如何获取屏幕亮度
get_brightness()方法返回当前亮度级别。
Syntax: get_brightness(display=None, method=None, verbose_error=False)
Parameters:
- display -: the specific display you wish to adjust. This can be the name, model, serial or index of the display
- method -: the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
- verbose_error -: a boolean value to control how much detail any error messages should contain
Returns: Current brightness level
Python3
# importing the module
import screen_brightness_control as sbc
# get current brightness value
current_brightness = sbc.get_brightness()
print(current_brightness)
# get the brightness of the primary display
primary_brightness = sbc.get_brightness(display=0)
print(primary_brightness)
Python3
# importing the module
import screen_brightness_control as sbc
# get current brightness value
print(sbc.get_brightness())
#set brightness to 50%
sbc.set_brightness(50)
print(sbc.get_brightness())
#set the brightness of the primary display to 75%
sbc.set_brightness(75, display=0)
print(sbc.get_brightness())
Python3
# importing the module
import screen_brightness_control as sbc
# get current brightness value
print(sbc.get_brightness())
# fade brightness from the current brightness to 50%
sbc.fade_brightness(50)
print(sbc.get_brightness())
# fade the brightness from 25% to 75%
sbc.fade_brightness(75, start = 25)
print(sbc.get_brightness())
# fade the brightness from the current
# value to 100% in steps of 10%
sbc.fade_brightness(100, increment = 10)
print(sbc.get_brightness())
Python3
# import the library
import screen_brightness_control as sbc
# get the monitor names
monitors = sbc.list_monitors()
print(monitors)
# now use this to adjust specific screens by name
sbc.set_brightness(25, display=monitors[0])
输出:假设亮度是这样的:
然后输出将是:
50
50
根据检测到的监视器数量,输出可以是列表或整数。
示例 2:如何设置屏幕亮度
set_brightness()方法改变屏幕的亮度。
Syntax: set_brightness(value, display=None, method=None, force=False, verbose_error=False, no_return=False)
Parameters:
- value: the level to set the brightness to. Can either be an integer or a string.
- display: the specific display you wish to adjust. This can be the name, model, serial or index of the display
- method: the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
- force (Linux only): If set to False then the brightness is never set to less than 1 because on Linux this often turns the screen off. If set to True then it will bypass this check
- verbose_error: A boolean value to control how much detail any error messages should contain
- no_return: if False, this function will return what the brightness was set to. If True, this function returns nothing, which is slightly quicker
蟒蛇3
# importing the module
import screen_brightness_control as sbc
# get current brightness value
print(sbc.get_brightness())
#set brightness to 50%
sbc.set_brightness(50)
print(sbc.get_brightness())
#set the brightness of the primary display to 75%
sbc.set_brightness(75, display=0)
print(sbc.get_brightness())
输出:
100
50
75
根据检测到的监视器数量,输出可以是列表或整数。
示例 3:如何淡化亮度
fade_brightness()方法将亮度轻轻地淡化到一个值。
Syntax: fade_brightness(finish, start=None, interval=0.01, increment=1, blocking=True)
Parameters:
- finish: The brightness value to fade to
- start: The value to start from. If not specified it defaults to the current brightness
- interval: The time interval between each step in brightness
- increment: The amount to change the brightness by each step
- blocking: If set to False it fades the brightness in a new thread
蟒蛇3
# importing the module
import screen_brightness_control as sbc
# get current brightness value
print(sbc.get_brightness())
# fade brightness from the current brightness to 50%
sbc.fade_brightness(50)
print(sbc.get_brightness())
# fade the brightness from 25% to 75%
sbc.fade_brightness(75, start = 25)
print(sbc.get_brightness())
# fade the brightness from the current
# value to 100% in steps of 10%
sbc.fade_brightness(100, increment = 10)
print(sbc.get_brightness())
输出:
75
50
75
100
示例 4:如何列出可用的监视器
list_monitors() 方法返回一个列表,其中包含所有检测到的监视器的名称
Syntax: list_monitors(method=None)
Parameters:
- method: the OS specific method to use. On Windows this can be “wmi” (for laptop displays) or “vcp” (for desktop monitors). On Linux this can be “light”, “xrandr”, “ddcutil” or “xbacklight”.
蟒蛇3
# import the library
import screen_brightness_control as sbc
# get the monitor names
monitors = sbc.list_monitors()
print(monitors)
# now use this to adjust specific screens by name
sbc.set_brightness(25, display=monitors[0])
输出:
["BenQ GL2450H", "Dell U2211H"]
显示器的名称和数量将根据插入计算机的内容而有所不同。