简体   繁体   中英

change backlight brightness in linux with python

I'm working on a python project which should be able to control my backlight brightness. I working with Ubuntu 17.04 and I already locate where the file is that show my backlight brightness

/sys/class/backlight/acpi_video0/brightness

the command that I can use in the bash terminal to change the value is

sudo su -c 'echo 12 > /sys/class/backlight/acpi_video0/brightness'

but I have no clue how to implement this in a py project. Maybe this is also the wrong way to start.

Thank you Guys for probably helping me out.

you can use either os.system() or subprocess.Popen()

Not really recommended, but I see no harm in it for a personal project where the input isn't coming from an external source.. That being said, one should take care using this, because you will be executing straight from your command line, so anything your CLI can do, this can do. You have been warned.

Using os.system() (you might have to prepend the path to your shell to the command. This is usually /bin/bash in Linux.):

import os os.system('echo "your command goes here"')

if that doesn't work, then it should look something like:

os.system('/bin/bash echo "your command goes here"')

Using subprocess.Popen() (again, you might need to prepend the path to your shell before the rest of the command.:

import subprocess subprocess.Popen('echo "your command goes here"')

Once again, I will say, this is NOT recomended for frequent usage especially where outside sources may affect the output of the command being ran. Only use this when you KNOW what will be input into the command.

In Ubuntu, I achieved this using xbacklight package and python's os.system() imported from os module.

Installation :

sudo apt install xbacklight

Python command :

os.system('xbacklight -set ' + str(value)) where value is the input.

So I do a bit of research and on a this site https://wiki.archlinux.org/index.php/backlight I have found the command

gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.freedesktop.DBus.Properties.Set org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 50>"

I have no idea how this works but i changed my backlight.

It only works on gnome!! but because i use gnome and the application should be for gnome it is OK for me

my function now looks like this:

def change_brightness(self, value):
    os.system('gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.freedesktop.DBus.Properties.Set org.gnome.SettingsDaemon.Power.Screen Brightness "<int32 ' + str(value) + '>"')

the value must be between 0 and 100

Try this:

def set_brightness(brightness):
    if int(brightness) > 15:
        raise TypeError("Need int 0 < and > 15")
    elif int(brightness) < 0:
        raise TypeError("Need int 0 < and > 15")
    with open("/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video0/brightness","w") as bright:
         bright.write(str(brightness))
         bright.close()
set_brightness(0) #Brightness 0-15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM