微软最近出了个 必应bing 缤纷桌面,使用下来还是不错,可以每天更换bing首页的北京作为壁纸,但是该软件有个不好的地方是,安装后桌面上会有一个搜索框出现,很是烦人,而且不能关掉。于是出于技术考虑,想到了使用python来实现这个功能。
正如很多介绍python书中那样,python是中胶水语言,用在哪里都是可行的。想要使用python给桌面设置背景只需要下个模块安装即可:
http://sourceforge.net/projects/pywin32/
代码非常简单,参考了网上一些其他人写了代码,具体代码如下:
# -*- coding: utf-8 -*- import urllib,time,os,image,win32gui,win32con,win32api class stealbing: def __init__(self): self.content = urllib.urlopen('http://cn.bing.com/').read() self.bgimageurl = '' self.localfilename = '' self.localbmpfilename = '' def parserimageurl(self): tempstr = self.content[self.content.index('g_img={url:')+12:] self.bgimageurl = tempstr[:tempstr.index('id:\'bgdiv\'')-2] def createlocalfilename(self): randomstr = time.strftime(%y%m%d, time.localtime()) self.localfilename = 'd:/bing/' + randomstr + '.jpg' self.localbmpfilename = 'd:/bing/' + randomstr + '.bmp' def downloadimage(self): if self.bgimageurl == '': self.parserimageurl() if self.localfilename == '': self.createlocalfilename() urllib.urlretrieve(self.bgimageurl, self.localfilename) def updatebgimage(self): img = image.open(self.localfilename) img.save(self.localbmpfilename) os.remove(self.localfilename) k = win32api.regopenkeyex(win32con.hkey_current_user,control panel\\desktop,0,win32con.key_set_value) win32api.regsetvalueex(k, wallpaperstyle, 0, win32con.reg_sz, 2) #2拉伸适应桌面,0桌面居中 win32api.regsetvalueex(k, tilewallpaper, 0, win32con.reg_sz, 0) win32gui.systemparametersinfo(win32con.spi_setdeskwallpaper, self.localbmpfilename , 1+2) if __name__ == '__main__': stealbing = stealbing() stealbing.downloadimage() stealbing.updatebgimage()