Ubuntu自動更換壁紙

個人博客:http://www.chenjianqu.com/

原文鏈接:http://www.chenjianqu.com/show-112.html

  現在Ubuntu16.04是我的主力系統,因此想用的舒服一點。我個人非常重視壁紙,好的壁紙令人心情愉悅。但是在Ubuntu上似乎沒有很好的壁紙管理工具,反正我沒找到滿意的。因此,這裏寫一個簡單的自動切換壁紙的python程序。

    程序思路是這樣的:在給定壁紙文件夾的情況下,通過python調用命令行實現壁紙設置。每隔30s切換一次壁紙。可以將文件夾內的壁紙標記爲“我喜歡”,然後也可以只切換我喜歡的壁紙。而且喜愛程度越高,該壁紙出現的概率越大。

    然後本文通過curses庫配置的終端進行交互。爲了不使用圖形界面?因爲我懶得寫,通過終端交互就夠了。

    代碼如下:

import os
import shutil
import commands
import random
import time
import curses
import locale#解決中文亂碼
import pickle

#a=97 A=65

if __name__=="__main__":
    
    path="/media/chen/chen/Photo/4k" #壁紙路徑
    dict_path="/media/chen/chen/Photo/4k.pkl" #配置關鍵保存目錄
    
    #定義全局變量
    names=os.listdir(path)
    love_list=list()
    love_dict=dict()
    is_run=True
    t=0
    last_name=''
    name=''
    mode=0 #0隨機切換壁紙模式 1切換喜歡的壁紙模式
    last_output=''
    
    #curses初始化
    locale.setlocale(locale.LC_ALL, '') #解決curses中文亂碼
    screen=curses.initscr()#初始化curses
    curses.noecho()#設置不回顯
    curses.cbreak()#設置不需要按回車立即響應
    screen.keypad(1)#開啓鍵盤模式
    screen.nodelay(1)  #阻塞模式讀取0 非阻塞 1
    
    #顯示幫助信息
    def display_help():
        global screen
        screen.clear()
        (h,w)=screen.getmaxyx()
        if(h<=12):
            return 0
        else:
            i=0
            screen.addstr(i, 0,"del:刪除該壁紙")
            i+=1
            screen.addstr(i, 0,"h:幫助信息")
            i+=1
            screen.addstr(i, 0,"l:喜歡該壁紙")
            i+=1
            screen.addstr(i, 0,"m:隨機的壁紙模式")
            i+=1
            screen.addstr(i, 0,"n:喜歡的壁紙模式")
            i+=1
            screen.addstr(i, 0,"o:輸出喜歡壁紙")
            i+=1
            screen.addstr(i, 0,"q:退出程序")
            i+=1
            screen.addstr(i, 0,"s:停止/繼續切換壁紙")
            i+=1
            screen.addstr(i, 0,"->:下一張壁紙")
            i+=1
            screen.addstr(i, 0,"<-:上一張壁紙")
            i+=1
            screen.refresh()
            return i
            
    #先顯示幫助信息,再顯示字符串s
    def display(s):
        global screen,last_output
        screen.clear()
        i=display_help()
        screen.addstr(i, 0,"上一條輸出:"+last_output)
        i+=1
        screen.addstr(i, 0,"當前的輸出:"+s)
        screen.refresh()
        last_output=s
    
    #輸出數據結構L的內容
    def display_list(L):
        global screen
        screen.clear()
        (h,w)=screen.getmaxyx()
        if(len(L)<h):
            for i,e in enumerate(L):
                screen.addstr(i,0,str(e[1])+' '+str(e[0]))
        else:
            for i,e in enumerate(L[-h:]):
                screen.addstr(i,0,str(e[1])+' '+str(e[0]))
        screen.refresh()
    
    #隨機獲取喜歡的壁紙
    def get_love_img():#根據喜歡的程序獲得圖片
        S=0
        for e in love_list:
            S+=e[1]
        r=random.randint(0,S)
        P=0
        for e in love_list:
            P+=e[1]
            if(r<P):
                return e[0]
        return love_list[0][0]
    
    #設置壁紙
    def SetWallpapers():
        global name,last_name,love_list,name,names,path,t,mode
        last_name=name
        if(mode==1 and len(love_list)>0):
            name=get_love_img()
        elif(mode==0):
            name=names[random.randint(0,len(names))]
        source_path = os.path.join(path, name)
        commands.getoutput(r'gsettings set org.gnome.desktop.background picture-uri "file:'+source_path+r'"')
        display(name)
        t=0
        
    #刷新喜歡的壁紙
    def updateLoveWallpapers():
        global love_list,love_dict,dict_path
        love_list= sorted(love_dict.items(),key=lambda x:x[1],reverse = True)
        with open(dict_path, 'wb') as f:
            pickle.dump(love_dict, f, pickle.HIGHEST_PROTOCOL)
            
    #不存在該文件 則創建
    if(not os.path.exists(dict_path)):
        with open(dict_path, 'wb') as f:
            pickle.dump(love_dict, f, pickle.HIGHEST_PROTOCOL)
    #否則加載字典
    else:
        with open(dict_path, 'rb') as f:
            love_dict=pickle.load(f)
        love_list= sorted(love_dict.items(),key=lambda x:x[1],reverse = True)

    #主進程
    while True:
        char=screen.getch()
        if(char==261):#'->' 下一張
            SetWallpapers()
            
        elif(char==260):#'<-' 上一張
            name=last_name
            source_path = os.path.join(path, name)
            commands.getoutput(r'gsettings set org.gnome.desktop.background picture-uri "file:'+source_path+r'"')
            display(name)
            t=0
            
        elif(char==113):#'q' 退出
            #恢復控制檯默認設置(若不恢復,會導致即使程序結束退出了,控制檯仍然是沒有回顯的)
            curses.nocbreak()
            screen.keypad(0)
            curses.echo()
            #結束窗口
            curses.endwin()
            break
            
        elif(char==330):#'del'刪除該壁紙
            if(mode==0):
                #文件刪除
                source_path = os.path.join(path, name)
                os.remove(source_path)
                display('已刪除 '+source_path)
                #更新壁紙列表
                names=os.listdir(path)
                #更新喜歡的壁紙
                if(name in love_dict.keys()):
                    love_dict.pop(name)
                    updateLoveWallpapers()
            elif(mode==1):
                love_dict.pop(name)
                updateLoveWallpapers()
                display('從喜歡壁紙中移除'+name)
            SetWallpapers()
            
        elif(char==115):#'s' 停止/繼續切換壁紙
            if(is_run):
                display("停止更換壁紙\n")
                is_run=False
            else:
                display("繼續更換壁紙\n")
                is_run=True
        
        elif(char==108):#'l' 喜歡該壁紙
            if(name not in love_dict.keys()):
                love_dict[name]=1
            else:
                love_dict[name]+=1
            updateLoveWallpapers()
            display('喜歡該壁紙')
        
        elif(char==109):#'m' 隨機切換壁紙模式
            mode=0
            display("隨機模式")
            
        elif(char==110):#'n' 切換喜歡壁紙模式
            mode=1
            display("喜歡模式")
        
        elif(char==111):#'o' 輸出喜歡的壁紙列表
            display_list(love_list)
        
        elif(char==104):#'h' 查看幫助信息
            display_help()
        
        if(is_run and t>300):#每隔30s更換壁紙
            SetWallpapers()
            
        time.sleep(0.1) #暫停0.1s
        t+=1

 需要注意的,當curses庫的addstr()函數輸出的內容超出終端窗口大小時,回報錯。因此通過getmaxxy()函數限制輸出的行數。

    比如我有如下的壁紙文件夾:

圖片1.jpg

    運行後主要的輸出信息:

圖片2.jpg

    查看喜歡的壁紙,下面的某圖片的喜歡次數越多,在喜歡的壁紙模式下,該圖片出現的概率越大

圖片3.jpg

    設置開機自啓動:新建一個.sh腳本,輸入python wallpaper.py,保存爲wallpaper.sh。然後在/etc/re.local中輸入bash /home/chen/wallpaper.sh &保存,即可實現開機啓動。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章