📌  相关文章
📜  如何使用Python控制笔记本电脑屏幕亮度?

📅  最后修改于: 2022-05-13 01:55:49.948000             🧑  作者: Mango

如何使用Python控制笔记本电脑屏幕亮度?

为了控制屏幕的亮度,我们使用了screen-brightness-control库。屏幕亮度控制库只有一个类,功能很少。下面提到了最有用的功能:

  1. get_brightness()
  2. 设置亮度()
  3. 淡出亮度()
  4. list_monitors()

安装:我们可以通过运行以下 pip 命令来安装包:

pip install screen-brightness-control

示例 1:如何获取屏幕亮度

get_brightness()方法返回当前亮度级别。

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()方法改变屏幕的亮度。

蟒蛇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()方法将亮度轻轻地淡化到一个值。

蟒蛇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() 方法返回一个列表,其中包含所有检测到的监视器的名称

蟒蛇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"]

显示器的名称和数量将根据插入计算机的内容而有所不同。