VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-control-laptop-screen-brightness-using-python/

⇱ How to Control Laptop Screen Brightness using Python? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Control Laptop Screen Brightness using Python?

Last Updated : 17 Aug, 2021

To control the brightness of the screen we are using the screen-brightness-control library. The screen-brightness-control library has only one class and few functions. The most useful functions are mentioned below:

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

Installation: We can install the package by running the following 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(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

 
 

Output: Suppose the brightness was this:

👁 Image

Then the output will be: 

50
50

The output can be a list or an integer depending on the number of detected monitors.
 

Example 2: How to Set Screen Brightness

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

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

 
   Output:

100
50
75

The output can be a list or an integer depending on the number of detected monitors.
 

  Example 3: How to Fade the Brightness

The fade_brightness() method gently fades the brightness to a value.

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

 
Output:

75
50
75
100

 Example 4: How to list available monitors

The list_monitors() method returns a list the names of all detected 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".

Output:

["BenQ GL2450H", "Dell U2211H"]

The names and quantity of monitors will vary depending on what is plugged into your computer.

Comment
Article Tags: