个人博客: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=65if __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=Truet=0last_name=''name=''mode=0 #0随机切换壁纸模式 1切换喜欢的壁纸模式last_output=''#curses初始化locale.setlocale(locale.LC_ALL, '') #解决curses中文乱码screen=curses.initscr()#初始化cursescurses.noecho()#设置不回显curses.cbreak()#设置不需要按回车立即响应screen.keypad(1)#开启键盘模式screen.nodelay(1)  #阻塞模式读取0 非阻塞 1#显示帮助信息def display_help():global screenscreen.clear()(h,w)=screen.getmaxyx()if(h<=12):return 0else:i=0screen.addstr(i, 0,"del:删除该壁纸")i+=1screen.addstr(i, 0,"h:帮助信息")i+=1screen.addstr(i, 0,"l:喜欢该壁纸")i+=1screen.addstr(i, 0,"m:随机的壁纸模式")i+=1screen.addstr(i, 0,"n:喜欢的壁纸模式")i+=1screen.addstr(i, 0,"o:输出喜欢壁纸")i+=1screen.addstr(i, 0,"q:退出程序")i+=1screen.addstr(i, 0,"s:停止/继续切换壁纸")i+=1screen.addstr(i, 0,"->:下一张壁纸")i+=1screen.addstr(i, 0,"<-:上一张壁纸")i+=1screen.refresh()return i#先显示帮助信息,再显示字符串sdef display(s):global screen,last_outputscreen.clear()i=display_help()screen.addstr(i, 0,"上一条输出:"+last_output)i+=1screen.addstr(i, 0,"当前的输出:"+s)screen.refresh()last_output=s#输出数据结构L的内容def display_list(L):global screenscreen.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=0for e in love_list:S+=e[1]r=random.randint(0,S)P=0for 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,modelast_name=nameif(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_pathlove_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_namesource_path = os.path.join(path, name)commands.getoutput(r'gsettings set org.gnome.desktop.background picture-uri "file:'+source_path+r'"')display(name)t=0elif(char==113):#'q' 退出#恢复控制台默认设置(若不恢复,会导致即使程序结束退出了,控制台仍然是没有回显的)curses.nocbreak()screen.keypad(0)curses.echo()#结束窗口curses.endwin()breakelif(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=Falseelse:display("继续更换壁纸\n")is_run=Trueelif(char==108):#'l' 喜欢该壁纸if(name not in love_dict.keys()):love_dict[name]=1else:love_dict[name]+=1updateLoveWallpapers()display('喜欢该壁纸')elif(char==109):#'m' 随机切换壁纸模式mode=0display("随机模式")elif(char==110):#'n' 切换喜欢壁纸模式mode=1display("喜欢模式")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.1st+=1

需要注意的,当curses库的addstr()函数输出的内容超出终端窗口大小时,回报错。因此通过getmaxxy()函数限制输出的行数。

比如我有如下的壁纸文件夹:

运行后主要的输出信息:

查看喜欢的壁纸,下面的某图片的喜欢次数越多,在喜欢的壁纸模式下,该图片出现的概率越大

设置开机自启动:新建一个.sh脚本,输入python wallpaper.py,保存为wallpaper.sh。然后在/etc/re.local中输入bash /home/chen/wallpaper.sh &保存,即可实现开机启动。

Ubuntu自动更换壁纸相关推荐

  1. 让 Ubuntu 桌面自动更换壁纸

    让 Ubuntu 桌面自动更换壁纸 Posted on 2016-07-10 22:56 京山游侠 阅读(4256) 评论(10) 编辑 收藏 引言 让我们的桌面系统自动更换壁纸是一个很常见的美化需求 ...

  2. Ubuntu 14.04自动更换壁纸

    Ubuntu 14.04自动更换壁纸 最近用ubuntu14.04,想添加一些自己拍的图片作为壁纸,并且让它自动更换. 查网上教程,知道其实背景图片是在文件夹/usr/share/background ...

  3. ubuntu进入桌面自动启动脚本_ubuntu 16.04LTS 开机启动自动更换壁纸的实现方法

    前言 上周电脑重装,换了ubuntu 16.04,想起来之前上课老师也是ubuntu而且他还提到他桌面是他自己写的个小脚本实现的自动更换桌面壁纸的,昨天晚上心血来潮自己网上搜了点资料实现了一下 = = ...

  4. ubuntu php xml模块,生成ubuntu自动切换壁纸xml文件的php代码

    运行代码后在图片目录下会生成yuxing.xml,方便ubuntu自动切换壁纸. /* * 生成ubuntu自动切换壁纸xml文件 */ //图片目录 $dir = '/home/yuxing/bac ...

  5. android 自动更换壁纸,安卓壁纸如何设置自动更换壁纸-手机天堂

    安卓壁纸是一款非常实用的手机壁纸更换软件,平台中有非常丰富的静态壁纸和视频动态壁纸,可以说是每天换一张都不会重样的,这就让手机变的更加的丰富多彩.相信有不少的朋友会认为老使用一张壁纸太单调,每天都换成 ...

  6. python修改桌面壁纸_利用Python对windows桌面自动更换壁纸

    根据时间星期几判断用哪张壁纸 # !/usr/bin/env python3 # -*- coding:utf-8 -*- import requests # import pywintypes im ...

  7. linux设置自动更换壁纸

    #!/bin/bash let n=0 files=($HOME/wallpapers/*.jpg) count=${#files[@]} while [ 1 ] dolet "n=n%$c ...

  8. linux 壁纸自动更换,使用LSWC在Linux中自动更换壁纸

    本文将为你介绍安装及使用LittleSimpleWallpaperChanger(LSWC)小脚本为你的Linux桌面定期自动更换壁纸.如果你不再使用LSWC,还可以使用本文介绍的删除方法处理. 其实 ...

  9. android 自动更换壁纸,超简单实用!教你安卓自动更换壁纸的方法

    爱美不只是女人的天性,喜新厌旧也算不上什么坏毛病.尤其如今手机分辨率越来越高.色彩表现也愈发给力,要是不给手机时不时换个高端大气上档次的壁纸,都对不起那块高科技屏幕.所以今天给大家带来安卓自动更换壁纸 ...

最新文章

  1. junit5_JUnit 5和Selenium –使用Selenium内置的`PageFactory`实现页面对象模式
  2. Assemby 打包并启动jar包
  3. 《深入理解计算机系统》CSAPP
  4. leetcode题库:2.两数相加
  5. RecycleView的Item Animator动画
  6. ESP8266 wifi钓鱼
  7. GPUImage实现水印
  8. html表格的形式制作调查问卷,问卷调查表格式,问卷调查怎么制作?
  9. vue 加headers_(vue.js)axios interceptors 拦截器中添加headers 属性
  10. 宇宙机器人超级计算机,宇宙机器人无线控制器使用指南白金攻略[多图]
  11. 机器学习(8)——回归和异常值处理(安然数据集)
  12. FileZilla连接ubuntu主机时选择21端口无法连接
  13. 《软件工程之美》打卡第六周,春招我借这份PDF的复习思路
  14. 基于matlab的傅里叶变换
  15. 我的Java后端书单1.0
  16. 【Fluent TUI】如何开启TUI命令的智能提示,如何搜索不知道的TUI命令;代码智能提示、code hinting、command prompt
  17. 微信小程序tabar页面不触发onShow
  18. 二战浙大失利+调剂科大教训帖
  19. win10系统下 ABBYY SDK安装及iKey激活
  20. 有趣的日语单词记忆法(外行学日语)

热门文章

  1. 宽带访问安全:电缆访问和 xDSL 线路--网络大典
  2. Go设计模式(17)-享元模式
  3. 认知-认知能力:认知能力
  4. web网站架构演变过程
  5. 验证码机制之验证码暴力破解
  6. Android 引入高德3D地图 显示白屏或黑屏解决办法
  7. 【图像去噪】基于matlab多种自适应均值滤波图像去噪【含Matlab 1843期】
  8. 2014 ChinaJoy落下帷幕 十大年度热门事件盘点
  9. 时间花在哪,价值就在哪
  10. Google支付常见错误