Related Articles

Related Articles

How to Control Laptop Screen Brightness using Python?
  • Last Updated : 11 Oct, 2020

To control the brightness of the screen we e are using the screen-brightness-control library. The screen-brightness-control library has only one class and three functions. The function are mention below:

  1. get_brightness()
  2. set_brightness()
  3. fade_brightness()

Installation: We can install the package by running the follwing pip command:

pip install screen-brightness-control

Example 1: How to Get Screen Brightness

The get_brightness() method returns the current brightness level.

Syntax: get_brightness(max_value=False, raw_value=False, verbose_error=False



Parameters:

  • max_value -: returns the maximum value the brightness can be set to. Always returns 100 on Windows. On Linux it returns the value stored in /sys/class/backlight/*/max_brightness if combined with raw_value=True
  • raw_value (Linux only) -: returns the value stored in /sys/class/backlight/*/brightness
  • verbose_error -: a boolean value to control how much detail any error messages should contain

Returns: Current brightness level

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import screen_brightness_control as sbc
  
# get current brightness  value
current_brightness = sbc.get_brightness()
print(current_brightness)
  
# always returns 100 on Windows. 
max_brightness = sbc.get_brightness(max_value = True)
print(max_brightness)

chevron_right


Output: Suppose the brightness was this:

Then the output will be:

50
100

Example 2: How to Set Screen Brightness

The set_brightness() method changes the brightness of the screen.



Syntax: set_brightness(brightness_level, force=False, raw_value=False, verbose_error=False)

Parameters:

  • brightness_level: the level to set the brightness to. Can either be an integer or a string.
  • 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
  • raw_value (Linux only): If set to ‘True’ then it attempts to write brightness_level directly to /sys/class/backlight/*/brightness. This will usually fail due to file permissions but it’s here if you need it.
  • verbose_error: A boolean value to control how much detail any error messages should contain

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import screen_brightness_control as sbc
  
# get current brightnessvalue
print(sbc.get_brightness())
  
#set brightness to 50%
sbc.set_brightness(50)
  
print(sbc.get_brightness())

chevron_right


  Output:

100
50

  Example 3: How to Fade the Brightness

The fade_brightness() method fades the brightness from start to finish in steps of increment, pausing for interval seconds between each step.

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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())

chevron_right


Output:

75
50
75
100

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :