import os,random,ctypes
path = r"D:/壁纸/夏天/"
bizhi = os.listdir(path)
n = random.randint(1,len(bizhi))
# 更换选中的壁纸
jpg = path + bizhi[n-1]
ctypes.windll.user32.SystemParametersInfoW(20, 0, jpg, 0)
print("壁纸已更换为:{}".format(jpg))
代码中的path替换为自己的壁纸路径即可。ctypes可能需要.net4.8,本机已安装,没有提示,可以直接用。
使用Python随机更换桌面壁纸
这段Python代码实现了从指定文件夹中随机选择一张图片作为桌面壁纸的功能。它首先获取指定路径下的所有文件名,然后利用random.randint选择一个索引,结合路径选取对应的jpg图片,最后通过ctypes.windll.user32.SystemParametersInfoW函数更改系统壁纸。
3806

被折叠的 条评论
为什么被折叠?



