How can I change the wallpaper using a Python script?

Posted by furtelwart on Ask Ubuntu See other posts from Ask Ubuntu or by furtelwart
Published on 2011-12-04T20:50:34Z Indexed on 2012/03/23 5:39 UTC
Read the original article Hit count: 293

Filed under:
|
|
|

I want to change my wallpaper in Ubuntu 11.10 (with Unity) in a small Python script. I found the possibility to change it via the gconf-editor in /desktop/gnome/background/picture_filename. With python-gconf, I'm able to change the necessary values.

Apparently, the gconf string is not read out. If I change it (either via a script or via gconf-editor), the wallpaper remains and in the menu of "Change wallpaper", the old wallpaper is shown.

How am I able to change the wallpaper for Unity via a Python script?

The following code does work. Apparently, the gsettings are only applied, if some Gtk code is executed.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Gtk, Gio

class BackgroundChanger():
        SCHEMA = 'org.gnome.desktop.background'
        KEY = 'picture-uri'

        def change_background(self, filename):
                gsettings = Gio.Settings.new(self.SCHEMA)
                print(gsettings.get_string(self.KEY))
                print(gsettings.set_string(self.KEY, "file://" + filename))
                Gtk.Window()
                print(gsettings.get_string(self.KEY))

if __name__ == "__main__":
        BackgroundChanger().change_background("/home/user/existing.jpg")

© Ask Ubuntu or respective owner

Related posts about 11.10

Related posts about unity