下面这段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
发布于 2014-10-05 03:00:54
对于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
对于下面的windows refer:
import ctypes
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "myimage.jpg" , 0)
发布于 2016-10-19 12:34:38
将"SystemParametersInfoA“更改为"SystemParametersInfoW”。这为我修复了桌面背景更新。
发布于 2017-07-05 07:20:13
是的,我知道我从2014年就开始挖掘这个主题了(哇!),但在遇到同样的问题后,我花了几个小时找出解决方案,结果是:由于.SystemParametersInfoW
留给我一个黑色的桌面,我决定继续使用.SystemParametersInfoA
,但使用cgi
模块将path变量编码为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)
希望它能帮上忙!
https://stackoverflow.com/questions/26196076
复制相似问题