Python桌面自定义---实现定时更换桌面壁纸
1 效果
大致效果如下,设置过定时更换壁纸的应该都知道是啥场景。
2 获取大量壁纸
可以参考之前写的一篇博客,全网采集壁纸360网站全网壁纸
一共采集了15个类型的壁纸
3 Python代码实现定时更换壁纸
涉及到的Python工具库,
import os
import random
import ctypes
import time
首先遍历获取指定文件夹下的所有壁纸图片
def file_name(self,file_dir):#获取指定目录下的所有jpg、gif格式的壁纸
filePathList = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.jpg' or os.path.splitext(file)[1] == ".gif":
filePathList.append(os.path.join(root, file))
return filePathList
接着将图片随机选择图片,设为壁纸,定时更换即可:
def main(self):
filePathList = self.file_name(r"C:\Users\Jason\Desktop\kwallpaper")
while True:
filePath = random.choice(filePathList) #随机选择一张图片
ctypes.windll.user32.SystemParametersInfoW(20, 0, filePath, 0) #设为壁纸
time.sleep(30) #间隔更换壁纸时间
整体代码:
# -*- coding: UTF-8 -*-
'''
@Author :Jason
@Desc :auto change wallpaper
复制代码的话只需修改下读取路径即可
'''
import os
import random
import ctypes
import time
class Change(object):
def file_name(self,file_dir):#获取指定目录下的所有jpg、gif格式的壁纸
filePathList = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.jpg' or os.path.splitext(file)[1] == ".gif":
filePathList.append(os.path.join(root, file))
return filePathList
def main(self):
filePathList = self.file_name(r"C:\Users\Jason\Desktop\kwallpaper")#修改这里即可使用
while True:
filePath = random.choice(filePathList) #随机选择一张图片
ctypes.windll.user32.SystemParametersInfoW(20, 0, filePath, 0) #设为壁纸
time.sleep(30) #间隔更换壁纸时间
if __name__ == "__main__":
Change().main()