用python设置windows壁纸不起作用

时间:2014-10-04 18:56:11

标签: python windows ctypes

以下一段python代码应该从Web下载BMP图像并将其保存到磁盘,然后将壁纸更改为下载的图像。壁纸更改应该是永久性的,即重新启动后不会恢复。这个函数是我使用pyinstaller编译成二进制exe的更大脚本的一部分。问题是,当我运行该程序时,应该更改壁纸的位不起作用,我在最后试图找出原因。有趣的是,如果我在python解释器中运行这个确切的代码,它按预期工作。此外,在以前版本的编译脚本中,壁纸更改顺利进行。任何评论,帮助,见解将不胜感激!

def wallpaper():
    try:
        os.chdir(launch_directory)
        urllib.urlretrieve('http://www.imagehost.com/image.bmp', 'image.bmp')
        ctypes.windll.user32.SystemParametersInfoA(20, 0, os.path.join(launch_directory, "image.bmp"), 1)
    except:
        pass

3 个答案:

答案 0 :(得分:1)

对于下面的Linux refer: -

import commands
command = "gconftool-2 --set /desktop/gnome/background/picture_filename --type string '/path/to/file.jpg'"
status, output = commands.getstatusoutput(command)  # status=0 if success

对于以下窗口refer: -

import ctypes
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "myimage.jpg" , 0)

答案 1 :(得分:1)

更改" SystemParametersInfoA"到" SystemParametersInfoW"。这为我修复了桌面后台更新。

答案 2 :(得分:1)

是的,我知道我正在挖掘2014年的这个帖子(哇!),但遇到了同样的问题,我花了几个小时搞清楚解决方案,这里是: 当.SystemParametersInfoW给我留下黑色桌面时,我决定坚持使用.SystemParametersInfoA,但使用us-ascii模块将路径变量编码为cgi。我写了下面这个函数,它最终像魅力一样:

import ctypes as c
import cgi
def set_as_wallpaper(path): 
    SPI = 20
    SPIF = 2
    return c.windll.user32.SystemParametersInfoA(SPI, 0, path.encode("us-ascii"), SPIF)

希望它有所帮助!

相关问题