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)
它的作品。
的文档状态:
如果删除为真(默认),该文件是只要它是封闭 删除。
在我的原始代码中,文件在自动关闭后退出with
语句的主体后才会被删除。那么为什么不是SPI_SETDESKWALLPAPER
工作?