天天看点

python实现动态壁纸_用Python实现动态的切换桌面背景

#!/usr/bin/python#-*- coding: utf-8 -*-

importos, os.path, fnmatch, commands#壁纸图片所在的目录

wallpaper_path = '/usr/share/backgrounds'

#搜索所有可用的壁纸

avail_wallpapers =[]for file inos.listdir(wallpaper_path):if not os.path.isfile(wallpaper_path + '/' +file):continue

if not fnmatch.fnmatch(file, '*.jpg') and\not fnmatch.fnmatch(file, '*.png'):continueavail_wallpapers.append('file://' + wallpaper_path + '/' +file)if len(avail_wallpapers) ==0:

[email protected]:~/python$ cat dynamic_wallpaper.py#!/usr/bin/python#-*- coding: utf-8 -*-

importos, os.path, fnmatch, commands#壁纸图片所在的目录

wallpaper_path = '/usr/share/backgrounds'

#搜索所有可用的壁纸

avail_wallpapers =[]for file inos.listdir(wallpaper_path):if not os.path.isfile(wallpaper_path + '/' +file):continue

if not fnmatch.fnmatch(file, '*.jpg') and\not fnmatch.fnmatch(file, '*.png'):continueavail_wallpapers.append('file://' + wallpaper_path + '/' +file)if len(avail_wallpapers) ==0:

exit#对所有可用的壁纸排序

avail_wallpapers.sort()#获取当前使用的壁纸

current_wallpaper = commands.getoutput('gsettings get org.gnome.desktop.background picture-uri').strip('\'')#计算下一张壁纸索引

try:

current_index=avail_wallpapers.index(current_wallpaper)except:

current_index= -1next_index= current_index + 1

if next_index >=len(avail_wallpapers):

next_index=0#设置下一张新壁纸

os.system('gsettings set org.gnome.desktop.background picture-uri \'' + avail_wallpapers[next_index] + '\'')