当前位置: 首页 > 编程文章 >

Ubuntu 16.04 LTS 开机自动更换壁纸的实现方法

Ubuntu 16.04 LTS 开机自动更换壁纸的实现方法

作者:江辰时间:2026-11-07 13:25:17浏览:674

本文介绍在 Ubuntu 16.04 LTS 系统中,通过 Python 脚本从必应壁纸接口爬取图片并自动设置桌面的方法。脚本包含图片数量阈值管理以节省磁盘空间,并处理离线环境下的启动间隔问题。同时提供 .desktop 文件配置步骤,实现系统开机时自动执行该壁纸更换任务。

本文目录展开

Ubuntu 16.04 自动更换壁纸实现方案

前言

近期重装了电脑并安装了 Ubuntu 16.04 系统。想起此前课程中老师曾演示过利用自定义小脚本实现桌面壁纸自动轮播的功能。受此启发,昨晚查阅相关资料后成功实现了这一效果。

功能

该功能的核心是从必应壁纸服务抓取最新的壁纸图片。为避免本地存储过多图片占用磁盘空间,脚本设置了最大图片数量阈值,一旦达到该限制,便会清理所有已下载的图片。随后通过 Python 的 os 模块调用系统命令来更新桌面背景。

具体更改 Unity 桌面背景的方法可参考 how-to-change-desktop-background-from-command-line-in-unity 。此外,考虑到系统刚启动时可能尚未连接网络,脚本中设置了爬取间隔机制以应对这种情况。

代码


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

# ---------------------------------------------------------- #
# This is a script which can change background automatically #
# every time the system starts.        #
# author: Huang Zhenyang          #
# email: 745125931@qq.com         #
# ---------------------------------------------------------- #
# ----- Import ----- #
import os
import re
import urllib
import time
import socket
# --- End Import --- #

class Spider(object):
 """
 This is the spider to get the img from being
 """
 def __init__( self, img_matched_pattern_para, url_para, file_name_para ):
  """
  init function
  :param img_matched_pattern_para: the pattern to match a img
  :param url_para: url to crawler
  :param file_name_para: file name
  """
  self.img_matched_pattern = img_matched_pattern_para
  self.url = url_para
  self.file_name = file_name_para
 def get_img(self):
  """
  download the image
  :return:
  """
  html = self.get_html()
  img_pos = re.search(self.img_matched_pattern, html)
  img_page_href = self.url + img_pos.group()[6:-7] + "download"
  urllib.urlretrieve(img_page_href, self.file_name)

 def get_html(self):
  """
  return the html
  :return: page's html 
  """
  page = urllib.urlopen(self.url)
  html = page.read()
  return html

class Controller(object):
 """
 This is the controller to control the spider's parameters.
 """
 def __init__(self, pattern_href_para, path_para, url_para, img_max_num_para):
  """
  :param pattern_href_para: href's match pattern
  :param path_para: path to save images
  :param url_para: url
  :param img_max_num_para: max number that the
  """
  self.pattern_href = pattern_href_para
  self.path = path_para
  self.url = url_para
  self.img_max_num = img_max_num_para
  self.file_name = ""
  self.init_file_name = "0.jpg"
  self.file_extension_name = ".jpg"
 def judge(self):
  """
  judge if the number of images is grater than img_max_num.
  if true, delete all of them and then run spider, else directly run spider.
  Also, we should set the file name.
  :return:
  """
  root = None
  _dirs = None
  files = None
  for root, _dirs, files in os.walk(self.path, True):
   pass
  files_len = len(files)
  if files_len == 10:
   for i in range(0, 10):
    os.remove(root + files[i])
   self.file_name = self.path + self.init_file_name
  else:
   self.file_name = self.path + str(files_len) + self.file_extension_name
 def run_spider(self):
  """
  run spider.
  TODO: This function needs to be modified in the future which makes these two class coupling too much.
  :return:
  """
  # In case user's computer hasn't connect the internet.
  for i in range(0, 60):
   try:
    spider = Spider(self.pattern_href, self.url, self.file_name)
    spider.get_img()
    break
   except IOError as e:
    print "Connection error: %s" % e
    time.sleep(60)
    continue
   except Exception as e:
    print "Connection error: %s" % e
    time.sleep(60)
    continue
  c_path = '"file://' + self.file_name + '"' # absolute path
  # call system command to change the gnome background
  os.system('gsettings set org.gnome.desktop.background picture-uri ' + c_path)
  print "gsettings set org.gnome.desktop.background picture-uri " + c_path

if __name__ == '__main__':
 pattern_href = r'href="/photo/.*?" rel="external nofollow" '
 path = '/home/hzy/图片/backgrounds/'
 url = 'https://bing.ioliu.cn/'
 img_max_num = 10
 controller = Controller(pattern_href, path, url, img_max_num)
 controller.judge()
 controller.run_spider()

开机自启动

要在 /home/hzy/.config/autostart 目录下实现开机自启,需新建一个 xxx.desktop 文件,其内容配置如下:


[Desktop Entry]
Name=autoChangeBackgroundImg
Comment=Python Program
Exec=python /home/hzy/Script/autoChangeBackgroundImg/autoChangeBackgroundImg.py
Icon=/home/hzy/Script/autoChangeBackgroundImg/autoChangeBackgroundImg.png
Terminal=false
MultipleArgs=false
Type=Application
Categories=Application;Development;
StartupNotify=true

说明:

1. Exec 字段后的路径指向实际运行的脚本位置。

2. Icon 字段可指定任意图片路径作为该启动项的图标。

以上即为 Ubuntu 16.04 LTS 下实现开机自动更换壁纸的完整方案,希望能为大家提供参考。

上一篇
Linux系统下MySQL服务的启动、停止与重启操作指南

本文介绍在Linux环境中对MySQL数据库进行状态检查及生命周期管理的常用命令。内容涵盖通过status或select version()查看版本,以及利用service、mysqld脚本和mysqladmin等工具执行启动、停止和重启操作的具体方法,适用于不同版本的MySQL服务管理需求。

下一篇
Ubuntu 12.04 x64 编译安装 LAMP (Apache, MySQL, PHP) 完整指南

本文详细记录了在 Ubuntu 12.04 x64 系统下,通过源码编译方式手动搭建 LAMP 环境的完整步骤。内容涵盖 Apache 2.4.2 及其依赖 APR、APR-Util、PCRE 和 zlib 的安装与常见错误排查,MySQL 5.5.25a 二进制包的解压配置及开机自启设置,以及 PHP 5.4.4 与 Apache 和 MySQL 的集成配置。针对编译过程中出现的 APR 未找到、mod_deflate 符号未定义、PCRE 缺失等典型报错提供了具体的解决方案。

相关文章

更多
Ubuntu下使用linuxdeployqt打包Qt程序的完整指南

在Ubuntu系统中,利用linuxdeployqt工具可将Qt应用程序打包为独立可执行文件,无需目标机器安装Qt环境。本文详细记录了从配置Qt环境变量、编译linuxdeployqt源码以绕过版本检查,到处理patchelf缺失及libjasper.so等动态库依赖错误的完整流程,确保打包过程顺利成功。

2025.04.17 997
Ubuntu安装CMake的几种方式及Android交叉编译解决方案

在Ubuntu中安装CMake通常采用apt源安装或源码编译两种方式。apt安装便捷但版本受限,如Ubuntu 16.04源中仅含3.5.1版,无法满足Android交叉编译所需的最低3.6.0要求;源码编译可获取最新版,但可能因Android SDK工具链不支持高版本而引发问题。通过建立Android SDK中CMake的软链接至系统路径,可有效解决版本冲突,确保第三方库顺利编译。

2025.12.21 995
Ubuntu服务器搭建PHP运行环境配置指南

在Ubuntu服务器上搭建PHP运行环境主要涉及Apache2、PHP模块及MySQL数据库的安装与配置。通过apt-get命令可快速完成核心组件部署,包括libapache2-mod-php5等必要模块。配置过程中需调整DocumentRoot指向开发目录或建立软链接,并使用init.d脚本重启服务以生效更改。

2025.10.17 994
Ubuntu下配置XAMPP开机自启动及解决MySQL.sock连接错误

在Ubuntu系统中安装XAMPP后,默认不具备开机自启动功能且MySQL客户端连接时常报错找不到socket文件。本文介绍通过编辑/etc/init.d/rc.local脚本实现XAMPP服务自动启动的方法,并针对Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'错误,提供创建符号链接以修正socket路径的解决方案,确保重启后服务正常可用。

2026.06.07 975
Ubuntu系统常见问题及解决办法

本文汇总了Ubuntu系统中常见的五类问题及其解决方案。针对文件夹创建权限不足,建议通过sudo命令或设置root密码提升权限;遇到Command 'vim' not found错误时,需根据是否残留文件选择直接安装或清理后重装;解决Windows与Ubuntu间复制粘贴失效,需重新安装open-vm-tools-desktop并重启;VMware中界面显示过小可通过安装VMware Tools实现自适应;若出现Command 'npm' not found提示,执行apt install npm即可补充缺失的支持文件。

2026.08.31 969
Ubuntu系统安装Docker完整指南

本文介绍在Ubuntu 14.04、16.04及16.10等64位版本中安装Docker的方法。主要涵盖通过官方APT仓库安装推荐方式,包括添加GPG密钥、配置源及指定版本安装;同时也提供下载DEB包手动安装的替代方案,并说明如何验证安装、升级及卸载Docker引擎。

2026.10.27 967

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。