温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python如何实现桌面壁纸切换功能

发布时间:2021-04-09 11:16:42 来源:亿速云 阅读:194 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关python如何实现桌面壁纸切换功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

大体分为两个部分

一、利用爬虫爬取壁纸

第一部分爬取图片url地址并且下载至本地
爬虫针对 http://image.so.com/ 【360壁纸写的】,如果要更换url地址自己改改

import requests
import json
import random
import os
#存放Ajax图片地址数据 
img_url_dict={}
#创建图片tmp文件夹
if not os.path.exists('image'):
  os.mkdir('image')
#爬取图片url地址
def getImgurl(root_url,sn):
  params={
    'ch': 'wallpaper',
    't1': 157,
    'sn': sn,
    'listtype': 'new',
    'temp': 1
  }
  headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko)Chrome/62.0 3202.62 Safari / 537.36'
  }
  try:
    response=requests.get(root_url,params=params,headers=headers)
  except RequestException:
    return None
  data=json.loads(response.text).get('list')
  img_url_list=[]
  for item in data:
    img_url_list.append(item.get('cover_imgurl'))
  img_url_dict[sn]=img_url_list
#下载图片
def download_image(name,image_url):
  try:
    response=requests.get(image_url)
  except RequestException:
    return "图像请求出错"
  file_name='{}/{}.{}'.format('image',name,'bmp');
  with open(file_name,'wb') as file:
    file.write(response.content)
#获取随机url地址并下载至image文件夹
def get_img():
  sn=30*random.randint(1,15)
  try:
    img_url_dict[sn]
  except KeyError:
    getImgurl('http://image.so.com/zj',sn)
  index=random.randint(0,len(img_url_dict[sn])-1)
  url=img_url_dict[sn][index]
  download_image('wallpaper',url)

二、更换桌面壁纸

第二部分将下载的图片作为壁纸,间隔一定时间重新下载,再切换壁纸
这部分借用python实现windows壁纸定期更换功能

import win32api, win32gui, win32con
import time
def setWallPaper(pic):
  # open register
  regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
  win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
  win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
  # refresh screen
  win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
if __name__=='__main__':
  while True:
    get_img()
    pic='your_path/image/wallpaper.bmp'#写绝对路径
    setWallPaper(pic)
    time.sleep(6)#6s切换一次壁纸

关于“python如何实现桌面壁纸切换功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI