web-dev-qa-db-ja.com

Pythonスクリプトを使用して壁紙を変更するにはどうすればよいですか?

Ubuntu 11.10(Unityを使用)の壁紙を小さなPythonスクリプトで変更したい。 gconf-editor/desktop/gnome/background/picture_filenameを介して変更する可能性を見つけました。 python-gconfを使用して、必要な値を変更できます。

どうやら、gconf文字列は読み取られません。 (スクリプトまたはgconf-editorを介して)変更すると、壁紙が残り、[壁紙の変更]メニューに古い壁紙が表示されます。

Pythonスクリプトを使用してUnityの壁紙を変更するにはどうすればよいですか?

次のコードは機能します。

#!/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 __== "__main__":
        BackgroundChanger().change_background("/home/user/existing.jpg")
11
guerda

残念ながら、gconfはそれ自体をうまくクリーンアップしません。それが古い設定です。 11.10のGNOME3とUnityでは、デスクトップの背景設定がdconfに保存されるようになりました。 dconf-editorを使用すると、org.gnome.desktop.background.picture-uriで設定を見つけることができます

Python、GTK、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()

GSettingsとPythonに関する役立つ2つのブログ投稿を以下に示します。

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

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

11
andrewsomething

どうぞ

#! /usr/bin/python

import os

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

たぶん最高ではなく、最も簡単な解決策:

import commands
command = 'gsettings set org.gnome.desktop.background picture-uri "file:///home/user/test.png"'
status, output = commands.getstatusoutput(command)
2
jochenh