• Python3实现Win10桌面背景自动切换


    [本文出自天外归云的博客园]

    得空写了个自动切换桌面背景图片的小程序。再不写python就要扔键盘了,对vue还有那么一点好感,天天php真是有够烦。

    准备工作

    准备个文件夹放在桌面上,平时看到什么高清好图就拽进去。

    运行脚本

    脚本如下:

    #!/usr/bin/python
    import ctypes
    import os
    import random import functools import schedule index = 0 def change_background(picture_path: str) -> None: ctypes.windll.user32.SystemParametersInfoW(20, 0, picture_path, 3) def get_pictures(dir_path: str) -> list: return [os.path.join(root, name) for root, dirs, files in os.walk(dir_path, topdown=False) for name in files if name.endswith('jpg') or name.endswith('png')] def log(text): def decorator(f): @functools.wraps(f) def wrap(*args, **kwargs): p = f(*args, **kwargs) print(f'{text}: {p}') return p return wrap return decorator @log(f'DESKTOP_BG_IMG switch to') def change_background_job(dir_path) -> None: if dir_path.__class__.__name__ == 'list': dir_path = dir_path[0] pictures = get_pictures(dir_path) index = random.randint(0, len(pictures) - 1) change_background(pictures[index]) return pictures[index] def scheduler(job: staticmethod, interval, arg_num, *args) -> None: if arg_num <= 0: schedule.every(interval).seconds.do(job) else: schedule.every(interval).seconds.do(job, [args[i] for i in range(arg_num)]) while True: schedule.run_pending() if __name__ == '__main__': scheduler(change_background_job, 10, 1, r'C:UserszenkilanDesktop est_pictures', 'hello', 'world')

    函数scheduler接受4个以上参数:

    1. 定时执行的job函数对象

    2. 执行时间间隔,单位:秒

    3. 函数job需要几个参数

    4~*. 函数job的参数们

    还可以进一步扩充,比如在get_pictures函数里面再加一些rules,低于多少mb的照片就不能作为桌面背景图之类的,接着加or就ok了。

  • 相关阅读:
    Json对象和Json字符串
    主界面的构造函数报错
    WPF DataGrid绑定的数据源更新后界面信息不更新
    WPF 数据绑定 ( 经验 )
    WPF 简单快速地去掉Button控件的边框
    字节数组与字符串形式的数字(序列号)之间的相互转换
    将十进制数字转换为字节数组
    WPF 调用线程无法访问此对象,因为另一个线程拥有该对象。
    cmd中一些命令
    Notepad++运行快捷键的设置
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/9715791.html
Copyright © 2020-2023  润新知