Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  12] [ 0]  / answers: 1 / hits: 10283  / 2 Years ago, mon, april 4, 2022, 2:08:47

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.



#!/usr/bin/python
# -*- 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.get_string(self.KEY))
print(gsettings.set_string(self.KEY, "file://" + filename))
gsettings.apply()
print(gsettings.get_string(self.KEY))

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

More From » unity

 Answers
1

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)
self.add(box)

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

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

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

self.add_filters(dialog)

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

dialog.destroy()

def add_filters(self, dialog):
filter_image = Gtk.FileFilter()
filter_image.set_name("Image files")
filter_image.add_mime_type("image/*")
dialog.add_filter(filter_image)

filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)

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


Here are two helpful blog posts on GSettings and Python:



http://www.micahcarrick.com/gsettings-python-gnome-3.html



http://www.lucidelectricdreams.com/2011/06/reading-and-writing-gsettings-from.html


[#41646] Wednesday, April 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
corsee

Total Points: 479
Total Questions: 122
Total Answers: 106

Location: Barbados
Member since Sat, May 9, 2020
4 Years ago
;