CentOS 6.3 上的动态壁纸更改

CentOS 6.3 上的动态壁纸更改

有没有可能的方法在 centos 6.3 和 gnome 2.28.2 上每五分钟更换一次壁纸(背景)?我找到了这个Python脚本:

#!/usr/bin/env python

import gobject
gobject.threads_init()
import gtk
import os
import xml.dom.minidom
import gconf

class Wallpaper:
    def __init__(self):
        # Create GTK Window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Dynamic Wallpaper")
        self.window.resize(350, 110)
        self.window.set_resizable(False)

        # Create window objects
        vbox = gtk.VBox()

        label = gtk.Label("Folder:")
        label.set_alignment(0.0, 0.5)
        self.folder_entry = gtk.Entry()
        button = gtk.Button("Select...")
        button.connect("clicked", self.button_clicked)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.folder_entry, False)
        hbox.pack_start(button, False)
        vbox.pack_start(hbox, False)

        label = gtk.Label("Picture duration:")
        label.set_alignment(0.0, 0.5)
        adj = gtk.Adjustment(5.0, 1.0, 60.0, 1.0, 1.0, 0.0)
        self.pdur_spinner = gtk.SpinButton(adj)
        self.pdur_combo = gtk.combo_box_new_text()
        self.pdur_combo.append_text("hr")
        self.pdur_combo.append_text("min")
        self.pdur_combo.append_text("sec")
        self.pdur_combo.set_active(1)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.pdur_spinner, False)
        hbox.pack_start(self.pdur_combo, False)
        vbox.pack_start(hbox, False)

        label = gtk.Label("Transition duration:")
        label.set_alignment(0.0, 0.5)
        adj = gtk.Adjustment(5.0, 0.0, 60.0, 1.0, 1.0, 0.0)
        self.tdur_spinner = gtk.SpinButton(adj)
        self.tdur_combo = gtk.combo_box_new_text()
        self.tdur_combo.append_text("hr")
        self.tdur_combo.append_text("min")
        self.tdur_combo.append_text("sec")
        self.tdur_combo.set_active(2)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.tdur_spinner, False)
        hbox.pack_start(self.tdur_combo, False)
        vbox.pack_start(hbox, False)

        self.cancel_button = gtk.Button(stock=gtk.STOCK_CLOSE)
        self.cancel_button.connect("clicked", self.destroy)
        self.ok_button = gtk.Button(stock=gtk.STOCK_OK)
        self.ok_button.set_sensitive(False)
        self.ok_button.connect("clicked", self.ok_button_clicked)
        hbox = gtk.HBox()
        hbox.pack_end(self.ok_button, False)
        hbox.pack_end(self.cancel_button, False)
        vbox.pack_end(hbox, False, False, 10)

        # Connect signal handlers
        self.window.connect("delete-event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        # Show entire window
        self.window.add(vbox)
        self.window.show_all()

    def destroy(self, widget):
        gtk.main_quit()

    def button_clicked(self, button):
        dialog = gtk.FileChooserDialog("Open..", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OK, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)
        dialog.set_filename(os.path.expanduser("~/Pictures/*"))
        response = dialog.run()
        filename = dialog.get_filename()
        dialog.destroy()

        if response == gtk.RESPONSE_OK:
            self.folder_entry.set_text(filename)
            self.files = []
            accepted = ('.jpg', '.jpeg', '.gif', '.png')
            for file in os.listdir(filename):
                if os.path.splitext(file)[1] in accepted:
                    self.files.append(os.path.join(filename, file))

            if len(self.files):
                self.ok_button.set_sensitive(True)
            else:
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = "Folder contains no images")
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()

    def ok_button_clicked(self, button):
        nfiles = len(self.files)

        if nfiles <= 0:
            return False

        folder = self.folder_entry.get_text()

        picture_duration = int(self.pdur_spinner.get_text())
        if self.pdur_combo.get_active_text() == "min":
            picture_duration = picture_duration * 60
        elif self.pdur_combo.get_active_text() == "hr":
            picture_duration = picture_duration * 60 * 60

        transition_duration = int(self.tdur_spinner.get_text())
        if self.tdur_combo.get_active_text() == "min":
            transition_duration = transition_duration * 60
        elif self.tdur_combo.get_active_text() == "hr":
            transition_duration = transition_duration * 60 * 60

        document = """<background><starttime><year>2009</year><month>08</month><day>04</day><hour>00</hour><minute>00</minute><second>00</second></starttime></background>"""
        dom = xml.dom.minidom.parseString(document)

        for i in range(0, nfiles):
            static = dom.createElement("static")
            duration = dom.createElement("duration")
            duration.appendChild(dom.createTextNode(str(picture_duration)))
            static.appendChild(duration)
            file = dom.createElement("file")
            file.appendChild(dom.createTextNode(self.files[i]))
            static.appendChild(file)
            dom.documentElement.appendChild(static)

            if transition_duration <= 0: continue

            transition = dom.createElement("transition")
            duration = dom.createElement("duration")
            duration.appendChild(dom.createTextNode(str(transition_duration)))
            transition.appendChild(duration)
            frm = dom.createElement("from")
            frm.appendChild(dom.createTextNode(self.files[i]))
            transition.appendChild(frm)
            to = dom.createElement("to")
            if i < nfiles-1:
                to.appendChild(dom.createTextNode(self.files[i+1]))
            else:
                to.appendChild(dom.createTextNode(self.files[0]))
            transition.appendChild(to)
            dom.documentElement.appendChild(transition)

        try:
            save_filename = os.path.join(folder, "background-1.xml")
            f = open(save_filename, 'w')
            f.write(dom.toxml("utf-8"))
            f.close()
        except IOError as (errno, strerror):
            try:
                save_filename = os.path.join(os.path.expanduser("~"), ".dynamic-wallpaper.xml")
                f = open(save_filename, 'w')
                f.write(dom.toxml("utf-8"))
                f.close()
            except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False
        except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False

        backgrounds_file = os.path.join(os.path.expanduser("~/.gnome2/"), "backgrounds.xml")
        doc = xml.dom.minidom.parse(backgrounds_file)

        wallpaper = dom.createElement("wallpaper")
        wallpaper.setAttribute("deleted", "false")
        name = dom.createElement("name")
        name.appendChild(dom.createTextNode("Dynamic Wallpaper"))
        filename = dom.createElement("filename")
        filename.appendChild(dom.createTextNode(save_filename))
        options = dom.createElement("options")
        options.appendChild(dom.createTextNode("stretched"))
        shade_type = dom.createElement("shade_type")
        shade_type.appendChild(dom.createTextNode("solid"))
        pcolor = dom.createElement("pcolor")
        pcolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
        scolor = dom.createElement("scolor")
        scolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
        wallpaper.appendChild(name)
        wallpaper.appendChild(filename)
        wallpaper.appendChild(options)
        wallpaper.appendChild(shade_type)
        wallpaper.appendChild(pcolor)
        wallpaper.appendChild(scolor)
        doc.documentElement.appendChild(wallpaper)

        try:
            f = open(backgrounds_file, 'w')
            f.write(doc.toxml("utf-8"))
            f.close()
        except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False

        conf_client = gconf.client_get_default()
        conf_client.add_dir("/desktop/gnome/background", gconf.CLIENT_PRELOAD_NONE)
        conf_client.set_string("/desktop/gnome/background/picture_filename", save_filename)
        conf_client.set_string("/desktop/gnome/background/picture_options", "strecthed")
        conf_client.set_string("/desktop/gnome/background/color_shading_type", "solid")
        conf_client.set_string("/desktop/gnome/background/primary_color", "#2c2c00001e1e")
        conf_client.set_string("/desktop/gnome/background/secondary_color", "#2c2c00001e1e")

        gtk.main_quit()

    def delete_event(self, widget, event):
        return False

if __name__ == "__main__":
    wallpaper = Wallpaper()
    gtk.main()

该脚本适用于 Ubuntu,当我尝试在我的系统上运行它时,它生成了一个 xml 文件,但它不起作用。

有谁能够帮助我?

答案1

我希望使用一个应用程序来执行此操作。以下是我过去在 Fedora 和 CentOS 上使用过的两种方法。

瓦帕波兹

是一个在 GNOME 任务栏中运行的小程序。

  • 您可以从以下位置下载 RPM:这里适用于各种 Fedora/CentOS 版本。
  • 它的主要网站是这里
  • 主要的 github 树是这里

沃尔帕波兹党卫军

外壳脚本

这是一个 bash 脚本,它将循环浏览图像列表。

#!/bin/bash
#Default values
pictdir="$HOME/Pictures"
time=1h

#Wallpaper names
fnme=('wp1.jpg' 'wp2.jpg' 'wp3.jpg')

fin=${#fnme[@]}
let bck=$fin-1

#Exit if the script is already running, just in case
#Got this part of script somewhere in this forum
if pidof -x $(basename $0) > /dev/null; then
  for p in $(pidof -x $(basename $0)); do
    if [ $p -ne $$ ]; then
      echo "Script $0 is already running: exiting"
      exit
    fi
  done
fi

#This is where the desktop wallpaper changes
for (( i=0;i<$fin;i++ )); do
    sleep $time #wait for $time second(s)
    gconftool-2 --type string --set /desktop/gnome/background/picture_filename $pictdir/${fnme[${i}]}
    if [ $i -eq $bck ]; then
         let i=-1
    fi
done

它利用gconftool-2.我还没试过这个CentOS 6.x但所以它可能需要一些调整,但我已经用过它了软呢帽CentOS 5.x。该脚本是礼貌的Linux问题线程

瓦尔奇

可用的这里。还没有尝试过,但据说它可以工作GNOME 2 & 3。 Wallch 的评论不错这里

党卫军瓦尔赫

相关内容