如何使用python根据时间更改桌面背景?

时间:2019-12-12 02:50:15

标签: python-3.x windows

所以我有这段代码可以根据当地时间更改背景。问题是,一旦代码运行,它就会停止,但是我希望它一直检查直到时间改变为止。因此,有一种方法可以添加一个while循环或任何一种for循环。另外,您如何将这个python文件添加到Windows中的任务中以使其永久运行?

代码:

import ctypes
import time

dawn_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_Dawn.jpg"
sunrise_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_Sunrise.jpg"
mid_day_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_MidDay.jpg"
dusk_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_Dusk.jpg"
night_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_Night.jpg"
midnight_wallpaper = "C:/Users/meena/Downloads/Backgrounds/Mac_Midnight.jpg"

result = time.localtime()
hours = result.tm_hour

if 3 <= hours <= 5:
   user32 = ctypes.WinDLL('user32', use_last_error=True).SystemParametersInfoW(20, 0, dawn_wallpaper, 0)
elif 6 <= hours <= 11:
   ctypes.windll.user32.SystemParametersInfoW(20, 0, sunrise_wallpaper, 0)
elif 12 <= hours <= 17:
   ctypes.windll.user32.SystemParametersInfoW(20, 0, mid_day_wallpaper, 0)
elif 18 <= hours <= 20:
   ctypes.windll.user32.SystemParametersInfoW(20, 0, dusk_wallpaper, 0)
elif 21 <= hours <= 22:
   ctypes.windll.user32.SystemParametersInfoW(20, 0, night_wallpaper, 0)
elif 23 <= hours <= 2:
   ctypes.windll.user32.SystemParametersInfoW(20, 0, midnight_wallpaper, 0)

0 个答案:

没有答案