SoFunction
Updated on 2024-11-15

python implement desktop wallpaper switching function

In this paper, we share the example of python desktop wallpaper to achieve the specific realization of the function of switching, for your reference, the specific content is as follows

It is roughly divided into two parts

First, the use of crawlers to crawl wallpaper

The first part crawls the image url address and downloads it locally.
crawler targeting / [360 wallpaper written], if you want to replace the url address to change their own change

import requests
import json
import random
import os
# Store Ajax image address data
img_url_dict={}
#Creating an image tmp folder
if not ('image'):
  ('image')
#crawl image url address
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=(root_url,params=params,headers=headers)
  except RequestException:
    return None
  data=().get('list')
  img_url_list=[]
  for item in data:
    img_url_list.append(('cover_imgurl'))
  img_url_dict[sn]=img_url_list
#Download Images
def download_image(name,image_url):
  try:
    response=(image_url)
  except RequestException:
    return "Image request error."
  file_name='{}/{}.{}'.format('image',name,'bmp');
  with open(file_name,'wb') as file:
    ()
# Get random url address and download to image folder
def get_img():
  sn=30*(1,15)
  try:
    img_url_dict[sn]
  except KeyError:
    getImgurl('/zj',sn)
  index=(0,len(img_url_dict[sn])-1)
  url=img_url_dict[sn][index]
  download_image('wallpaper',url)

Second, replace the desktop wallpaper

The second part will download the image as a wallpaper, re-download it at certain intervals, and then switch the wallpaper
borrow from this sectionpython implement windows wallpaper regular change function

import win32api, win32gui, win32con
import time
def setWallPaper(pic):
  # open register
  regKey = (win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
  (regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
  (regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
  # refresh screen
  (win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
if __name__=='__main__':
  while True:
    get_img()
    pic='your_path/image/'# Write absolute paths
    setWallPaper(pic)
    (6)#6sSwitch wallpaper once

This is the whole content of this article.