LoginSignup
1
3

More than 3 years have passed since last update.

夜には星空の壁紙を(Python)

Last updated at Posted at 2020-07-28

Windowsの壁紙を時刻に応じて変えるスクリプトを作ってみた話。

ポイント:

  • ctypes.windll.user32.SystemParametersInfoWで壁紙を変えられる。
  • 定期実行は無限ループ + 1秒スリープで十分。
  • 実行はpythonwから実行すると、バックグラウンドで動くので良い(止めるときにtasks managerからしか止められないのは難点、しかもプロセス名がpythonwとなって分かりにくい)。
  • タスクスケジューラーでスタートアップ時のタスクに登録しておけば、起動時に自動起動できる。
import ctypes
import os
import datetime
import time


image_dir = r'D:\wallpaper'
current_image_name = None

while True:
    now = datetime.datetime.now()

    if now.hour < 6 or 19 <= now.hour:
        new_image_name = 'star.png'
    elif 17 <= now.hour:
        new_image_name = 'sunset.jpeg'
    else:
        new_image_name = 'daytime.jpeg'

    if new_image_name != current_image_name:
        abs_file_name = os.path.join(image_dir , new_image_name)
        ctypes.windll.user32.SystemParametersInfoW(20, 0,  abs_file_name, 0)
        current_image_name = new_image_name

    time.sleep(1)  # check every 1 sec

切り替え時にちらつくので、変更の有無をチェックして、変更があったときのみSystemParametersInfoWを実行する。

季節ごとに変えても面白いかも。壁紙が窓のようになって、時間や季節が感じられると良いですね。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3