通过python更改Windows 10中的桌面背景

时间:2016-12-02 21:34:02

标签: windows python-3.x windows-10

我正在为自己做一个小项目,我撞墙了。我需要在Windows 10 64位上更改桌面背景。我试图使用下面的脚本根据本地图像更改背景。代码执行没有错误,但桌面只是变黑。我仔细检查过,我的图片位于c:\CuratedWallpaper\Mario.bmp,所以这不是问题。

import ctypes

directory = "c:\CuratedWallpaper"
imagePath = directory + "\Mario.bmp"

def changeBG(imagePath):
    SPI_SETDESKWALLPAPER = 20
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagePath , 0)
    return;

changeBG(imagePath)

1 个答案:

答案 0 :(得分:2)

我使用SystemParametersInfoW而不是SystemParametersInfoA,如下所示:

ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3)

这是ANSI vs UNICODE路径字符串的问题。

它适用于Windows 10。