Welcome 微信登录

首页 / 操作系统 / Linux / 自己动手写Python实现Ubuntu自动切换壁纸

使用Windows 7一段时间,觉得它的自动换壁纸也挺好用的,换到Ubuntu 11.04上,本想装个软件让它自动换,后来发现Drapes运行不了,又懒得装其他软件了。于是想按照别人说的写个shell自动换壁纸,但是因为偶没怎么接触过shell编程,所以就求助于python了。Ubuntu原本就可以支持自动换壁纸,我们在外观首选项下的背景项可以看到原本就有个宇宙的幻灯片。这个幻灯片主要靠xml定义,位于/usr/share/backgrounds/cosmos/下的background-1.xml,我们打开它可以看到:
  1. <starttime>  
  2.   <year>2009</year>  
  3.   <month>08</month>  
  4.   <day>04</day>  
  5.   <hour>00</hour>  
  6.   <minute>00</minute>  
  7.   <second>00</second>  
  8. </starttime>  
这个是设置幻灯片开始时间,只要设为过去或者现在就可以马上开始了。于是下面就有很多类是这样的:
  1. <static>  
  2.     <duration>1795.0</duration>  
  3.     <file>/usr/share/backgrounds/cosmos/cloud.jpg</file>  
  4.   </static>  
  5.   <transition>  
  6.     <duration>5.0</duration>  
  7.     <from>/usr/share/backgrounds/cosmos/cloud.jpg</from>  
  8.     <to>/usr/share/backgrounds/cosmos/comet.jpg</to>  
  9.   </transition>  
  10.   <static>  
  11.     <duration>1795.0</duration>  
  12.     <file>/usr/share/backgrounds/cosmos/comet.jpg</file>  
  13.   </static>  
  14.   <transition>  
  15.     <duration>5.0</duration>  
  16.     <from>/usr/share/backgrounds/cosmos/comet.jpg</from>  
  17.     <to>/usr/share/backgrounds/cosmos/earth-horizon.jpg</to>  
  18.   </transition>  
static标签下的duration是设置一张图保持多久,transition同理,两者加起来就是一张图显示的时间了,1795 + 5 = 1800秒,即 30 分钟。然后如果要循环播放的话,最后一个transtion要跳回第一个即可。虽然我们可以手写这个xml,但是实在太恶心了。所以我们求助于python自动生成了。
  1. # -*-coding:utf-8-*-   
  2. # 作者:华亮   
  3. import os  
  4.   
  5. xml = """"" 
  6. <background> 
  7.   <starttime> 
  8.     <year>2009</year> 
  9.     <month>08</month> 
  10.     <day>04</day> 
  11.     <hour>00</hour> 
  12.     <minute>00</minute> 
  13.     <second>00</second> 
  14.   </starttime> 
  15. """  
  16.   
  17. static_duration = 1795      # 一张壁纸的停留时间   
  18. trasition_duration = 5      # 切换时间www.linuxidc.com   
  19.   
  20. def CreateStatic(duration, file):  
  21.     return "<static> <duration>" + str(duration) + "</duration> <file>" + str(file) + "</file> </static> "    
  22.   
  23. def CreateTransition(duration, fromFile, toFile):  
  24.     return "<transition> <duration>" + str(duration) + "</duration> <from>" + str(fromFile) + "</from> <to>" + str(toFile) + "</to> </transition> "  
  25. # 读取当前目录下所有文件   
  26. images = []  
  27. for filename in os.listdir(os.getcwd()):  
  28.     root, ext = os.path.splitext(filename)  
  29.     if ext.lower() == ".bmp" or ".jpg" or ".png":  
  30.         images.append(os.path.join(os.getcwd(), filename))  
  31. # 生成XML   
  32. for i in range(len(images) - 1):  
  33.     xml += CreateStatic(static_duration, images[i]) + CreateTransition(trasition_duration, images[i], images[i + 1])  
  34. xml += CreateStatic(static_duration, images[len(images) - 1]) + CreateTransition(trasition_duration, images[len(images) - 1], images[0]) + "</background>"  
  35. # 保存文件       
  36. file = open(os.path.basename(os.getcwd()) + ".xml""w")  
  37. file.write(xml)  
  38. file.close()  
  39.           
将这个py文件放到图片的目录下,保存为back.py,然后在shell里运行:python back.py,随后会生成以这个目录命名的xml文件,我们就打开外观首选项,添加刚刚生成的xml即可。



自己动手,丰衣足食~