• 【熟能生巧】Windows 自动更换壁纸


    问题引出

    个人习惯在白天的时候使用亮色系的风景壁纸,但是在晚上的时候就显得太刺眼了,需要更换为暗色系的壁纸。
    每次手动更换不免觉得费心,于是便想写一个脚本自动化这个过程。

    解决思路

    在Windows系统下,写一个python脚本更换壁纸,并设置在开机时,以及每一个小时自动触发该脚本。
    该脚本可根据当前系统时间,判断是白天还是夜晚,从而随机选择不同的壁纸。

    代码说明

    使用time模块获取系统时间,设置6:00am-6:00pm为白天,其它时间段为夜晚,
    创建两个folder,day and night,分别存放不同系列图片,根据时间随机调用该folder下的图片。
    关于更换壁纸,使用了win32api, win32con, win32gui这几个模块。

    import win32api, win32con, win32gui
    import random
    import datetime
    import time
    import os
    
    folder_path_day = r'C:UsersAdministrator.WIN-1J9R69V4VDRDesktopgday\'
    folder_path_ngt = r'C:UsersAdministrator.WIN-1J9R69V4VDRDesktopg
    ight\'
    
    # https://blog.csdn.net/felixzh123/article/details/76085132
    def set_wallpaper(img_path):
        # 打开指定注册表路径
        reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\Desktop", 0, win32con.KEY_SET_VALUE)
        # 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
        win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "6")
        # 最后的参数:1表示平铺,拉伸居中等都是0
        win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
        # 刷新桌面
        win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
    
    def count_pic_number(input_path):
        count=0
        for (root, dirs, files) in os.walk(input_path): 
            for filename in files:
                count+=1
        print(count)
        return count
    	
    if __name__ == '__main__':
    	# get current hour
    	current_time = time.localtime(time.time())
    	current_hour = current_time[3]
    
    	# get image according to day or night
    	if current_hour > 6 and current_hour < 18:
    		print ("day")
    		folder_path = folder_path_day
    		pic_count_day = count_pic_number(folder_path)
    		pic_number = random.randint(1,pic_count_day)
    	else:
    		print("night")
    		folder_path = folder_path_ngt
    		pic_count_ngt = count_pic_number(folder_path)
    		pic_number = random.randint(1,pic_count_ngt)
    	# get image absolute path
    	pic_format = '.jpg'
    	pic_path = folder_path + str(pic_number) + pic_format
    	# change the wallpaper
    	set_wallpaper(pic_path)
    

    如何运行

    • 创建folder,放入图片
    • 修改参数 folder_path_day, folder_path_ngt
    • python app.py

    自启动

    PART 1:开机启动

    • 写一个.bat文件,invoke python script,放于Startup目录下,C:UsersAdministratorAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup。
    python C:your_location_toapp.py
    

    PART 2:定时启动

    • 任务计划程序 -- 创建任务。
    • 注意:程序这一栏填的是python解释器的路径,参数写的是python脚本的路径。

    参见:Windows定时任务设置

  • 相关阅读:
    一些恶作剧的vbs程序代码
    The Weather
    UEditor (富文本编译器)
    Redis 存储数组
    QQ第三方登录(完结篇)
    QQ第三方登录(预备节)
    谷歌浏览器添加JSON-Handle插件
    Nginx 之防盗链配置
    noi,ac第五场部分题解 By cellur925
    NOIp2002神经网络 【拓扑排序】By cellur925
  • 原文地址:https://www.cnblogs.com/maxstack/p/9323291.html
Copyright © 2020-2023  润新知