歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Ubuntu下實現用Python開機自動更新壁紙為bing壁紙

Ubuntu下實現用Python開機自動更新壁紙為bing壁紙

日期:2017/3/1 9:42:23   编辑:Linux編程

因為用的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, 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 )

#點擊自動更新按鈕事件
def OnButtonAutoFrush( self ):
try:
#創建desktop文件放在啟動文件夾下
file = open("/home/%s/.config/autostart/autobing.desktop" % (getpass.getuser()) , 'w')
desktop = """[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=AutoBing
Type=Application
Exec=python "%s/%s" one
Terminal=false
Comment=auto change systembackground from bingimage
NoDisplay=false
Categories=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 sys
app = QtGui.QApplication(sys.argv)
myqq = BingWindow()
myqq.show()
sys.exit(app.exec_())

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved