我该如何使用Python更改我的桌面背景?
我想在Windows和Linux系统中实现此操作。
我该如何使用Python更改我的桌面背景?
我想在Windows和Linux系统中实现此操作。
import ctypes
import os
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, 'your image path', 3)
#'C:\\Users\\Public\\Pictures\\abc.jpg'
对我来说它很好用。Windows10,Python27。
在使用Python2.5或更高版本的Windows操作系统上,使用ctypes加载user32.dll并调用相关函数。
import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,"Path_wallpaper", 0)
speak("Background changed succesfully")
你可以使用这个库PyWallpaper,在我的Mac上也能用。
安装方法:输入pip install PyWallpaper
。
然后就可以改变/设置你的壁纸了 -
from PyWallpaper import change_wallpaper
change_wallpaper("/some_path/sample.jpg")
为ShivaGuntuku的帖子添加一点精确信息: 在Python 3中,您应该将'SytemParametersInfoA'中的'A'替换为'W'。下面是一个小例子,用Python 3更改Windows10桌面背景:
import ctypes
import os
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(
SPI_SETDESKWALLPAPER, 0, 'C:\\Users\\godet\\OneDrive\\Images\\breaker_wall.jpg', 0)
这对我有效
import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,path:os.PathLike,3)