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.

# -*- coding: utf-8 -*-
from gi.repository import Gio

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

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

if __name__ == "__main__":

Unfortunately, gconf doesn't really clean up after itself very well. That's and old setting. With GNOME3 and Unity in 11.10, the desktop background setting is now stored in dconf. With dconf-editor you can find the setting at org.gnome.desktop.background.picture-uri

Here's a quick example showing how to change the background with python, GTK, and GObject Introspection:

#! /usr/bin/python

from gi.repository import Gtk, Gio

class BackgroundChanger(Gtk.Window):

    SCHEMA = 'org.gnome.desktop.background'
    KEY = 'picture-uri'

    def __init__(self):
        Gtk.Window.__init__(self, title="Background Changer")

        box = Gtk.Box(spacing=6)

        button1 = Gtk.Button("Set Background Image")
        button1.connect("clicked", self.on_file_clicked)

    def on_file_clicked(self, widget):
        gsettings = Gio.Settings.new(self.SCHEMA)

        dialog = Gtk.FileChooserDialog("Please choose a file", self,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))


        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            background = dialog.get_filename()
            gsettings.set_string(self.KEY, "file://" + background)
        elif response == Gtk.ResponseType.CANCEL:


    def add_filters(self, dialog):
        filter_image = Gtk.FileFilter()
        filter_image.set_name("Image files")

        filter_any = Gtk.FileFilter()
        filter_any.set_name("Any files")

win = BackgroundChanger()
win.connect("delete-event", Gtk.main_quit)

Here are two helpful blog posts on GSettings and Python:



  • Thank you for the hint. It seems correct, but unfortunately, the settings are not updated. If I set the new URI, the function returns true for success, but in dconf-editor or a get-string call, the old value is returned. Therefore, the wallpaper is not updated. Am I doing this wrong?
    – guerda
    Dec 14 '11 at 12:11
  • Hmmm... The above code certainly updates the wallpaper. Maybe if you posted some of your code on a pastebin somewhere I'd have a better idea what's going on in your case. Dec 14 '11 at 17:34
  • Hi andrewsomething! Thanks for coming back to me! I updated my question with my code example. Would be great if you could help me.
    – guerda
    Dec 15 '11 at 18:14
  • Your code works for me... Thank you for your help! I will post a minimal working example, too.
    – guerda
    Dec 15 '11 at 19:34

Here you go

#! /usr/bin/python

import os

os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/user/Pictures/wallpaper/Stairslwallpaper.png")

Maybe not the best but the easiest solution:

import commands
command = 'gsettings set org.gnome.desktop.background picture-uri "file:///home/user/test.png"'
status, output = commands.getstatusoutput(command)
  • 2
    This is the terminal command only. Maybe you should include how to invoke it via Python to fully answer the question?
    – N.N.
    Jan 4 '12 at 14:46
  • no, this is a python script that works exactly that way.
    – dirkk0
    Jan 1 '17 at 19:58
  • 1
    Just saw that 'commands' is deprecated. w = "/usr/share/backgrounds/warty-final-ubuntu.png"; c = 'gsettings set org.gnome.desktop.background picture-uri "file://%s"' % w; import subprocess; subprocess.call(c.split())
    – dirkk0
    Jan 2 '17 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.