Automatically update wallpaper with Python on Ubuntu as bing Wallpaper

Source: Internet
Author: User
Because the WindowsPhone mobile phone is used, there is a mobile phone lock screen which is automatically updated to bing wallpaper every day. It is quite easy to use, so I want to implement this function under Ubuntu, I have been tossing for a week or two intermittently. However, in the end, all the functions are implemented. The main functions are automatic update and manual refresh. The image is automatically downloaded to the user's image directory. Self-written Python Ubuntu automatic switch wallpaper http://www.linuxidc.com/Linux/2

Because Windows Phone's mobile Phone is used, there is a mobile Phone lock screen which is automatically updated to bing's wallpaper every day. It works quite well, so I want to implement this function under Ubuntu, I have been tossing for a week or two intermittently. However, in the end, all the functions are implemented. The main functions are automatic update and manual refresh. The image is automatically downloaded to the user's image directory.

Self-written Python Ubuntu automatic switch wallpaper http://www.linuxidc.com/Linux/2011-08/41500.htm

Python core programming version 2. (Wesley J. Chun). [High Definition PDF Chinese edition] http://www.linuxidc.com/Linux/2013-06/85425.htm

(Zhou Wei, Zong Jie). [hd PDF scan version + book with video + code] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python script to get Linux information http://www.linuxidc.com/Linux/2013-08/88531.htm

Build a desktop algorithm transaction research environment http://www.linuxidc.com/Linux/2013-11/92534.htm with Python in Ubuntu
 
Code Implementation:

#-*-Coding: UTF-8 -*-

# Form implementation generated from reading ui file 'background. 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
T AttributeError:
Def _ fromUtf8 (s ):
Return s

Try:
_ Encoding = QtGui. QApplication. UnicodeUTF8
Def _ translate (context, text, disambig ):
Return QtGui. QApplication. translate (context, text, disambig, _ encoding)
T AttributeError:
Def _ translate (context, text, disambig ):
Return QtGui. QApplication. translate (context, text, disambig)


# Defining a master URL
BingURL = 'HTTP: // cn.bing.com'
# Define RSSURL
RssURL = 'HTTP: // www.bing.com/HPImageArchive.aspx? Format = xml & idx = 0 & n = 8'
# Define the image URL
ImageURL =''


'''
Get DOM object and node through BING RSS
Save the spliced IMAGE path to the local device and call
Terminal: Execute the command to set the BACKGROUND
'''
Def updateBack ():
# Retrieving RSS feeds
Root = ET. fromstring (urllib. urlopen (rssURL). read ())
# Find the latest BING wallpaper URL
Img = root. getiterator ('image') [0]. find ('url'). text
# Obtain the user name to splice the image path
User_name = getpass. getuser ()
# Obtain the image number as the name of the Local Image
Name = root. getiterator ('image') [0]. find ('fullstartdate'). text
# Splicing Images
ImageURL = bingURL + img
# Download images
Urllib. urlretrieve (imageURL, R'/home/% s/image/%s.jpg '% (user_name, name ))
# Set background
OS. system ('gsettings set org. gnome. desktop. background picture-uri "file: // home/qing/image/developers.jpg" '% (name ))

Class Ui_MainWindow (QtGui. QMainWindow ):
Def setupUi (self, MainWindow ):
Try:
# Test whether the startup is enabled. If yes, directly update the background and exit the program.
Sys. argv [1]
UpdateBack ()
Sys. exit ()

Except t Exception, e:
# Otherwise, it is determined to be manually started.
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)
# Link Click Event
Self. connect (self. pushButton, QtCore. SIGNAL ('clicked () '), self. OnButtonFrush)
Self. connect (self. pushButton2, QtCore. SIGNAL ('clicked () '), self. OnButtonAutoFrush)

 

# Click the auto update button event
Def OnButtonAutoFrush (self ):
Try:
# Create a desktop file and place it in the startup folder.
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' prompt ', U' automatic update setting successful \ n if the program path is moved, Please reset ')

Except t Exception, e:
QtGui. QMessageBox. information (self, U' prompt ', u'' set automatic update failed ''')
Raise e

# Click to refresh the desktop Wallpaper
Def OnButtonFrush (self ):
Try:
UpdateBack ()
QtGui. QMessageBox. information (self, U' prompt ', u''' BING wallpaper updated successfully ''')
Pass
Except t Exception, e:
QtGui. QMessageBox. information (self, U' prompt ', u''' update failed ''')
Raise

Def retranslateUi (self, MainWindow ):
MainWindow. setWindowTitle (_ translate ("MainWindow", "BING wallpaper automatically changed", None ))
Self. pushButton. setText (_ translate ("MainWindow", "manual refresh", 'pushutton '))
Self. pushButton2.setText (_ translate ("MainWindow", "Automatic Login refresh", 'pushutton2 '))

Class BingWindow (QtGui. QMainWindow ):
# Initialization Interface
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.exe c _())

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2014-06/103854.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.