温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python3实现Win10桌面自动切换的方法

发布时间:2020-08-12 11:58:08 来源:亿速云 阅读:246 作者:小新 栏目:开发技术

这篇文章主要介绍Python3实现Win10桌面自动切换的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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

准备工作

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

Python3实现Win10桌面自动切换的方法

运行脚本

脚本如下:

#!/usr/bin/python
import ctypes
import osimport 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:\Users\zenkilan\Desktop\test_pictures', 'hello', 'world')

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

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

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

3. 函数job需要几个参数

4~*. 函数job的参数们

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

以上是Python3实现Win10桌面自动切换的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI