zoukankan      html  css  js  c++  java
  • Python设置桌面壁纸

    代码出处

    关注公众号【一行数据】后台回复【一行05】下载本文所有源代码和exe

    五行Python代码自动换你的电脑桌面壁纸(内附源码和exe) - 云+社区 - 腾讯云
    https://cloud.tencent.com/developer/article/1661753

    基于Python实现Windows下壁纸切换功能_一个人的孤落时辰-CSDN博客
    https://blog.csdn.net/qinyuanpei/article/details/79279831

    import urllib.request
    import requests
    import os.path
    import ctypes
    
    def get_img_url(raw_img_url="https://area.sinaapp.com/bingImg/"):
        r = requests.get(raw_img_url)
        img_url = r.url  # 得到图片文件的网址
        print('img_url:', img_url)
        return img_url
    
    def save_img(img_url, dirname):
        # 保存图片到磁盘文件夹dirname中
        try:
            if not os.path.exists(dirname):
                print('文件夹', dirname, '不存在,重新建立')
                # os.mkdir(dirname)
                os.makedirs(dirname)
            # 获得图片文件名,包括后缀
            basename = "bing.jpg"
            # 拼接目录与文件名,得到图片路径
            filepath = os.path.join(dirname, basename)
            # 下载图片,并保存到文件夹中
            urllib.request.urlretrieve(img_url, filepath)
        except IOError as e:
            print('文件操作失败', e)
        except Exception as e:
            print('错误 :', e)
        print("Save", filepath, "successfully!")
    
        return filepath
    
    def set_img_as_wallpaper(filepath):
        ctypes.windll.user32.SystemParametersInfoW(20, 0, filepath, 3)
    
    def main():
        dirname = "C:/一行数据/更换壁纸/"  # 图片要被保存在的位置
        img_url = get_img_url()
        filepath = save_img(img_url, dirname)  # 图片文件的的路径
        print(filepath)
        set_img_as_wallpaper(filepath)
    main()

    转载仅为学习,不会商用。
    欢迎转载原创,附文链接。
  • 相关阅读:
    Daemon——守护进程
    RTMP
    CR LF CR/LF
    SO_REUSEADDR
    [aac @ ...] more samples than frame size (avcodec_encode_audio2)
    前端向后端传数据的方法
    控制层接受参数
    Swagger2
    net.sf.json------json解析
    springboot
  • 原文地址:https://www.cnblogs.com/xdd1997/p/14187591.html
Copyright © 2011-2022 走看看