代码:
import urllib.request
import tempfile
import shutil
import ctypes
SPI_SETDESKWALLPAPER = 20
with urllib.request.urlopen('https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png') as response, tempfile.NamedTemporaryFile() as f:
shutil.copyfileobj(response, f)
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, f.name, 0)
但是,如果您使用tempfile.NamedTemporaryFile(delete=False)
,它可以工作。你知道吗
文档状态:
If delete is true (the default), the file is deleted as soon as it is closed.
在我的原始代码中,只有在退出with
语句体并自动关闭之后,文件才会被删除。那为什么SPI_SETDESKWALLPAPER
不起作用呢?你知道吗
我发现了问题:
首先,需要更改
fWinIni
参数的值:这样可以在注销后保留壁纸。你知道吗
其次,需要关闭temp文件以使
SystemParametersInfoW
工作。因此,delete=False
是必要的。你知道吗最后,使用
os.remove(f.name)
手动删除临时文件。你知道吗您需要阅读文档中接下来的两句话,内容如下:
相关问题 更多 >
编程相关推荐