我正在编写一个简单的(我认为是)程序,为一周中的每一天设置不同的桌面背景。它运行时没有任何错误,但什么也没有发生。图像的路径有效。有什么想法吗?
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20
localtime = time.localtime(time.time())
wkd = localtime[6]
if wkd == 6:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\1.jpg",0)
elif wkd == 0:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\2.jpg",0)
elif wkd == 1:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\3.jpg",0)
elif wkd == 2:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\4.jpg",0)
elif wkd == 3:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\5.jpg",0)
elif wkd == 4:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\6.jpg",0)
elif wkd == 5:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\7.jpg",0)发布于 2015-05-04 03:45:38
我一定已经阅读了关于这个主题的所有现有站点,在放弃之前,我读到了这个工作代码(win7 pro 64位,Python3.4)
import ctypes
SPI_SETDESKWALLPAPER = 0x14 #which command (20)
SPIF_UPDATEINIFILE = 0x2 #forces instant update
src = r"D:\Downloads\_wallpapers\3D-graphics_Line_025147_.jpg" #full file location
#in python 3.4 you have to add 'r' before "path\img.jpg"
print(ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, src, SPIF_UPDATEINIFILE))它使用SystemParametersInfoW代替SystemParametersInfoA (W代替A)。
希望它能帮助你和许多其他有类似问题的人。
发布于 2016-12-31 15:10:58
如果你使用的是Python3,你应该使用ctypes.windll.user32.SystemParametersInfoW而不是ctypes.windll.user32.SystemParametersInfoA( this answer说的是W而不是A)。Another answer对此进行了描述,因为在Python3中,str类型的形式是UTF-16,就像C中的wchar_t *一样。
更重要的是,请最小化代码,如下所示:
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20
wallpapers = r"C:\Users\Owner\Documents\Wallpaper\%d.jpg"
localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallpapers%(wkd+1), 0)不要重复你的话。
发布于 2014-02-12 08:21:41
这不是对你的问题的回答,但是你通常可以通过这样做来缩小你的程序并删除冗余:
import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20
wallpapers = [
r"C:\Users\Owner\Documents\Wallpaper\1.jpg",
r"C:\Users\Owner\Documents\Wallpaper\2.jpg",
r"C:\Users\Owner\Documents\Wallpaper\3.jpg",
r"C:\Users\Owner\Documents\Wallpaper\4.jpg",
r"C:\Users\Owner\Documents\Wallpaper\5.jpg",
r"C:\Users\Owner\Documents\Wallpaper\6.jpg",
r"C:\Users\Owner\Documents\Wallpaper\7.jpg",
]
localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, wallpapers[wkd], 0)https://stackoverflow.com/questions/21715895
复制相似问题