用Python實現動態切換案頭背景

來源:互聯網
上載者:User

前兩天看了一篇文章《自己寫指令碼自動更換案頭》,覺得用Python實現起來應該更容易理解,於是就有了下面的dynamic-wallpaper.py指令碼。

 

dynamic-wallpaper.py指令碼的完整內容:

#!/usr/bin/python# -*- coding: utf-8 -*-import os, os.path, fnmatch, commands# 壁紙圖片所在的目錄wallpaper_path = '/usr/share/backgrounds'# 搜尋所有可用的壁紙avail_wallpapers = []for file in os.listdir(wallpaper_path):    if not os.path.isfile(wallpaper_path + '/' + file):        continue    if not fnmatch.fnmatch(file, '*.jpg') and \       not fnmatch.fnmatch(file, '*.png'):        continue    avail_wallpapers.append('file://' + wallpaper_path + '/' + file)if len(avail_wallpapers) == 0:amonest@amonest-virtual-machine:~/python$ cat dynamic_wallpaper.py#!/usr/bin/python# -*- coding: utf-8 -*-import os, os.path, fnmatch, commands# 壁紙圖片所在的目錄wallpaper_path = '/usr/share/backgrounds'# 搜尋所有可用的壁紙avail_wallpapers = []for file in os.listdir(wallpaper_path):    if not os.path.isfile(wallpaper_path + '/' + file):        continue    if not fnmatch.fnmatch(file, '*.jpg') and \       not fnmatch.fnmatch(file, '*.png'):        continue    avail_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 + 1if next_index >= len(avail_wallpapers):    next_index = 0# 設定下一張新壁紙os.system('gsettings set org.gnome.desktop.background picture-uri \'' + avail_wallpapers[next_index] + '\'')
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.