ubuntu下实现用python开机自动更新壁纸为bing壁纸

因为用的windows phone的手机,里面有一个手机锁屏每天自动更新为bing的壁纸,用着挺好的,遂想在ubuntu下实现这个功能,断断续续折腾了一两个星期,惭愧。不过好在最终还是将所有的功能实现了。主要的功能有开机自动更新,以及手动刷新。图片会自动下载到用户的图片目录下面。

自己动手写python实现ubuntu自动切换壁纸 http://www.linuxidc.com/linux/2011-08/41500.htm

《python核心编程 第二版》.(wesley j. chun ).[高清pdf中文版] http://www.linuxidc.com/linux/2013-06/85425.htm

《python开发技术详解》.( 周伟,宗杰).[高清pdf扫描版+随书视频+代码] http://www.linuxidc.com/linux/2013-11/92693.htm

python脚本获取linux系统信息 http://www.linuxidc.com/linux/2013-08/88531.htm

在ubuntu下用python搭建桌面算法交易研究环境 http://www.linuxidc.com/linux/2013-11/92534.htm 代码实现:

# -*- 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, qtguiimport urllibimport osimport getpassfrom xml.etree import elementtree as et

try: _fromutf8 = qtcore.qstring.fromutf8except 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)

#定义主urlbingurl = ‘http://cn.bing.com’#定义rssurlrssurl = ‘http://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=8’#定义图片地址urlimageurl = ”

”’通过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 )

#点击自动更新按钮事件 def onbuttonautofrush( self ): try: #创建desktop文件放在启动文件夹下 file = open(“/home/%s/.config/autostart/autobing.desktop” % (getpass.getuser()) , ‘w’) desktop = “””[desktop entry]version=1.0encoding=utf-8name=autobingtype=applicationexec=python “%s/%s” oneterminal=falsecomment=auto change systembackground from bingimagenodisplay=falsecategories=utility; “”” % (os.getcwd() , os.path.basename(__file__)) file.write(desktop) file.close() qtgui.qmessagebox.information( self, u’提示’, u’自动更新设置成功\n如果移动了程序路径请重新设置’)

except exception, e: qtgui.qmessagebox.information( self, u’提示’, u”’设置自动更新失败”’) raise e

#点击刷新桌面壁纸 def onbuttonfrush(self): try: updateback() qtgui.qmessagebox.information( self, u’提示’, u”’bing壁纸更新成功”’) pass except exception, e: qtgui.qmessagebox.information( self, u’提示’, u”’更新失败”’) raise

def retranslateui(self, mainwindow): mainwindow.setwindowtitle(_translate(“mainwindow”, “bing壁纸自动更换”, none)) self.pushbutton.settext(_translate(“mainwindow”, “手动刷新”, ‘pushbutton’)) self.pushbutton2.settext(_translate(“mainwindow”, “登陆自动刷新”, ‘pushbutton2’))

class bingwindow(qtgui.qmainwindow): #初始化界面 def __init__(self,parent=none): qtgui.qwidget.__init__(self,parent) self.madwindow()

def madwindow(self): self.ui = ui_mainwindow() self.ui.setupui(self)

import sysapp = qtgui.qapplication(sys.argv) myqq = bingwindow() myqq.show()sys.exit(app.exec_())

更多ubuntu相关信息见ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址:http://www.linuxidc.com/linux/2014-06/103854.htm

Posted in 未分类

发表评论