Python3如何實現(xiàn)Win10桌面自動切換
得空寫了個自動切換桌面背景圖片的小程序。再不寫python就要扔鍵盤了,對vue還有那么一點好感,天天php真是有夠煩。
準備工作
準備個文件夾放在桌面上,平時看到什么高清好圖就拽進去。

運行腳本
腳本如下:
#!/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')
函數(shù)scheduler接受4個以上參數(shù):
1. 定時執(zhí)行的job函數(shù)對象
2. 執(zhí)行時間間隔,單位:秒
3. 函數(shù)job需要幾個參數(shù)
4~*. 函數(shù)job的參數(shù)們
還可以進一步擴充,比如在get_pictures函數(shù)里面再加一些rules,低于多少mb的照片就不能作為桌面背景圖之類的,接著加or就ok了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python基于遞歸算法實現(xiàn)的漢諾塔與Fibonacci數(shù)列示例
這篇文章主要介紹了Python基于遞歸算法實現(xiàn)的漢諾塔與Fibonacci數(shù)列,結合實例形式分析了漢諾塔與Fibonacci數(shù)列的遞歸實現(xiàn)技巧,需要的朋友可以參考下2018-04-04
keras 使用Lambda 快速新建層 添加多個參數(shù)操作
這篇文章主要介紹了keras 使用Lambda 快速新建層 添加多個參數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實現(xiàn)注冊登錄
這篇文章主要介紹了Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實現(xiàn)注冊登錄。下面文章著情介紹,需要的小伙伴可以參考一下2022-01-01

