使用方法
代码: 全选
先要给可执行权限
sudo chmod +x background.py
包里面有两个文件,一个主程序background.py,一个说明文档README
具体参数的使用方法README里面有,一共才3个参数,很简单的,注释是中文的,方便大家学习
有什么BUG、疑问、建议、批评的尽管说,我全部接受!
使用示例:
代码: 全选
./background.py 3 /home/youself/picture/
懒得下载的话,这个是源代码 嘿嘿
代码: 全选
#!/usr/bin/env python
#-*- coding: utf8 -*-
import random, os, os.path, sys, pynotify, time, imghdr, gconf
### 帮助文本 ###
def help():
print '''
Background Switcher, v0.01
created by: zkwlx
Thank : Bummer@companyName.here
*这是一个定时切换壁纸的脚本
*使用:python ./background.py [等待时间] [壁纸路径]
或python ./background.py [-h or --help]
注意!上面两种参数不能混用!
*所有参数都是可选的,其中:
上面第二个是显示该帮助文档
第一个中[等待时间]就是切换壁纸的时间间隔,以'分'为单位,缺省为10分钟
[壁纸路径]是您指定的文件夹,切换的就是此文件夹里的图片,缺省为脚本所在文件夹
*对了,这个脚本是无限循环切换壁纸,所以想退出只能Ctrl+C或直接关闭终端了……
*谢谢下载和使用^_^!
'''
sys.exit()
### 提示信息 ###
def notify(subject, message = None):
n = pynotify.Notification(subject, message)
n.show()
### 主程序 ###
def main():
if len(sys.argv) == 2 and \
(sys.argv[1] == '-h' or sys.argv[1] == '--help'):
help()
all_images = []
sleep_time = 600
images_dir = sys.path[0]
try:
################ 解析参数 ################
if len(sys.argv) == 3 and os.path.isdir(sys.argv[2]):
sleep_time = int(sys.argv[1]) * 60
images_dir = sys.argv[2]
elif len(sys.argv) == 2 and os.path.isdir(sys.argv[1]):
images_dir = sys.argv[1]
elif len(sys.argv) == 2:
sleep_time = int(sys.argv[1]) * 60
############### 创建壁纸列表 #############
if images_dir[-1] != '/':
images_dir += '/'
for files in os.listdir(images_dir):
if os.path.isfile(os.path.join(images_dir + files)) \
and imghdr.what(os.path.join(images_dir, files)):
all_images.append(os.path.join(images_dir, files))
############### 开始循环换壁纸 #########
client = gconf.client_get_default()
num = len(all_images) - 1
while True:
image = all_images[random.randint(0, num)]
time.sleep(sleep_time)
client.set_string('/desktop/gnome/background/picture_filename', image)
notify('当前壁纸为:', os.path.basename(image))
finally :
notify('程序异常或用户强行退出。', '感谢使用!^_^')
if __name__ == '__main__':
main()