gue*_*rda 11 wallpaper python unity
我想用一个小的 Python 脚本在 Ubuntu 11.10(使用 Unity)中更改我的墙纸。我发现可以通过gconf-editorin更改它/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 __name__ == "__main__":
BackgroundChanger().change_background("/home/user/existing.jpg")
Run Code Online (Sandbox Code Playgroud)
and*_*ing 11
不幸的是,gconf 并没有很好地清理自己。那是旧的设置。在 GNOME3 和 Unity 11.10 中,桌面背景设置现在存储在 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()
Run Code Online (Sandbox Code Playgroud)
以下是关于 GSettings 和 Python 的两篇有用的博客文章:
http://www.micahcarrick.com/gsettings-python-gnome-3.html
http://www.lucidelectricdreams.com/2011/06/reading-and-writing-gsettings-from.html
小智 8
干得好
#! /usr/bin/python
import os
os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/user/Pictures/wallpaper/Stairslwallpaper.png")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9545 次 |
| 最近记录: |