Python实现自动换壁纸功能

        Python实现自动替换壁纸功能,原理相当简单,看代码就可懂

目录详情如下:

Python实现自动换壁纸功能_第1张图片

XML配置,目前配置了路径和刷新时间:



	pictures
	5

翠花,上代码:

# -*- coding: UTF-8 -*-
import random
import ctypes
import time
import os
from xml.dom.minidom import parse
import datetime

picture_list = []
path = ''
random_time = 60


def build_path(args) :
	end_path = os.getcwd()
	for arg in args :
		end_path = end_path + '\\' + arg
	return end_path

def init_pic() :
	g = os.walk(build_path([path]))
	#print('==== %s' %path)
	for _,_,filelist in g :
		for filename in filelist:
			if filename.endswith('jpg'):
				picture_list.append(filename)
	if picture_list :
		return True
	return False

#随机图片
def random_pic_index() :
	return picture_list[random.randint(0, len(picture_list)-1)]

#读XML配置
def read_xml() :
	doc = parse('./config.xml')
	root = doc.documentElement
	global path, random_time
	path = root.getElementsByTagName('path')[0].firstChild.data
	random_time = int(root.getElementsByTagName('random_time')[0].firstChild.data)
	#print(path, random_time)


def main() :
	read_xml()
	if not init_pic() :
		print('Init error, not have pictures(filename with .jpg)')
		exit (1)
	while True :
		filename = random_pic_index()
		ctypes.windll.user32.SystemParametersInfoW(20,  0, build_path([path, filename]), 0)  #替换壁纸
		print('%s  picture: %s' %(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'), filename))
		time.sleep(random_time)

main()

 

来几张效果图吧:

 

你可能感兴趣的:(python)