得空寫了個自動切換桌面背景圖片的小程序。再不寫python就要扔鍵盤了,對vue還有那么一點好感,天天php真是有夠煩。
準備工作
準備個文件夾放在桌面上,平時看到什么高清好圖就拽進去。
運行腳本
腳本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/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了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/LanTianYou/p/9715791.html