为什么通过python更改桌面会导致红色墙纸?

2024-06-12 10:48:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的脚本,它可以在/r/wallpaps上获得最热门的帖子,并将其下载到一个目录中,然后将其设置为我的Windows10壁纸

但是,每次我运行脚本时,我的壁纸都会变红。如果我用鼠标右键点击我下载的图片,并将其设置为我的墙纸,效果会很好

import praw
import urllib.request
import os
import ctypes

def main():
    change_wallpaper(get_image())

def change_wallpaper(url):
    SPI_SETDESKWALLPAPER = 20
    directory = os.path.dirname(os.path.realpath(__file__)) + "\\bin\\" + 'image.jpg'
    urllib.request.urlretrieve(url,directory)

    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, directory, 0)


def get_image():
    reddit = praw.Reddit(client_id='id', client_secret='secret', user_agent='app')
    get_hottest = reddit.subreddit('wallpapers').hot(limit=1)

    for post in get_hottest:
        url = post.url
        title = post.title
        author = post.author
    return url
main()

Tags: imageimport脚本urlgetosmainrequest