下面的 python 代码应该从网络下载 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
最佳答案
对于 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)
关于python - 使用 python 设置 Windows 壁纸不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196076/