因为用的Windows Phone的手机,里面有一个手机锁屏每天自动更新为bing的壁纸,用着挺好的,遂想在Ubuntu下实现这个功能,断断续续折腾了一两个星期,惭愧。不过好在最终还是将所有的功能实现了。主要的功能有开机自动更新,以及手动刷新。图片会自动下载到用户的图片目录下面。
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]
在Ubuntu下用Python搭建桌面算法交易研究环境
代码实现:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'backGroundC.ui'
#
# Created: Sat Jun 21 13:16:32 2014
# by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
import urllib
import os
import getpass
from xml.etree import ElementTree as ET
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
#定义主URL
bingURL = 'http://cn.bing.com'
#定义RSSURL
rssURL = 'http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=8'
#定义图片地址URL
imageURL = ''
'''
通过BING的RSS得到DOM对象,获取节点
后拼接IMAGE路径保存到本地然后调用
Terminal执行设定BACKGROUND的命令
'''
def updateBack():
#获取RSS源
root = ET.fromstring( urllib.urlopen( rssURL ).read( ) )
#查到最新的一张BING壁纸URL
img = root.getiterator ('image')[0].find('url').text
#获取用户名,用来拼接图片路径
user_name = getpass.getuser()
#获取图片编号用来当作本地图片的名称
name = root.getiterator ('image')[0].find('fullstartdate').text
#拼接图片
imageURL = bingURL + img
#下载图片
urllib.urlretrieve(imageURL, r'/home/%s/图片/%s.jpg'%( user_name, name))
#设置背景
os.system('gsettings set org.gnome.desktop.background picture-uri "file:///home/qing/图片/%s.jpg"' % ( name ) )
class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
try:
#测试是否是开机启动,是的话直接更新背景完成后退出程序
sys.argv[1]
updateBack()
sys.exit()
except Exception, e:
#否则判定为手动启动
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(297, 130)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 10, 281, 41))
self.pushButton.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton2= QtGui.QPushButton(self.centralwidget)
self.pushButton2.setGeometry(QtCore.QRect(10, 60, 281, 41))
self.pushButton2.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.pushButton2.setObjectName(_fromUtf8("pushButton2"))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#链接点击事件
self.connect( self.pushButton, QtCore.SIGNAL( 'clicked()' ), self.OnButtonFrush )
self.connect( self.pushButton2, QtCore.SIGNAL( 'clicked()' ), self.OnButtonAutoFrush )