python 注册表操作

https://blog.csdn.net/xufive/article/details/104106627

# -*- coding: utf-8 -*-

import os, random
import win32api, win32gui, win32con


def set_wallpaper(photo_path):
    """设置壁纸"""

    # 1.打开注册表键
    key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)

    # 2.设置壁纸风格:0=居中 1=平铺 2=拉伸
    win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "2")

    # 3.设置壁纸是否缩放:0=缩放 1=原图
    win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")

    # 4.设置壁纸
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, photo_path, 3)

    # 5.关闭注册表键
    win32api.RegCloseKey(key)


def set_wallpaper_random(photo_dir):
    """随机设置壁纸"""

    wall_papers = list()
    for root, dirs, files in os.walk(photo_dir):
        for name in files:
            if os.path.splitext(name)[1].lower() == ".jpg":
                wall_papers.append(os.path.join(root, name))

    set_wallpaper(random.choice(wall_papers))


if __name__ == '__main__':
    # photo_path = r'D:\CSDN\Column\desktop\album\20200129150646.jpg'
    # set_wallpaper(photo_path)
    set_wallpaper_random(r'C:\MapDownload\googlemaps\satellite\15\26486')

猜你喜欢

转载自www.cnblogs.com/gisoracle/p/12276082.html