设置窗口壁纸与python不工作

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

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

对于Linux, 请参阅以下内容:

 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 

对于窗户请参考下面:

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

将“SystemParametersInfoA”更改为“SystemParametersInfoW”。 这为我修复了桌面背景更新。

是的,我知道我从2014年(哇!)挖这个线程,但遇到同样的问题,我花了几个小时找出解决方案,这里是:因为.SystemParametersInfoW离开我一个黑色桌面,我决定坚持使用.SystemParametersInfoA但使用cgi模块将路径变量编码为us-ascii 。 我写了下面这个功能,最后像魅力一样工作:

 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) 

希望能帮助到你!