如何使用Python更改桌面背景?

52

我该如何使用Python更改我的桌面背景?

我想在Windows和Linux系统中实现此操作。


2
对于你提出的关于 Linux 的问题(假设你使用 GNOME 桌面环境),你可能想要查看 http://oracle.bridgewayconsulting.com.au/~danni/misc/change-background-py.html。 - unutbu
有人知道如何在KDE中实现这个吗? - TheInitializer
15个回答

2

更改桌面背景图片

    import ctypes
    import os
    SPI_SETDESKWALLPAPER = 20 
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, 'your image path', 3) 
    #'C:\\Users\\Public\\Pictures\\abc.jpg'

对我来说它很好用。Windows10,Python27。


2

在使用Python2.5或更高版本的Windows操作系统上,使用ctypes加载user32.dll并调用相关函数。

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,"Path_wallpaper", 0) 
     speak("Background changed succesfully")

1

你可以使用这个库PyWallpaper,在我的Mac上也能用。

安装方法:输入pip install PyWallpaper。 然后就可以改变/设置你的壁纸了 -

from PyWallpaper import change_wallpaper
change_wallpaper("/some_path/sample.jpg")

1

为ShivaGuntuku的帖子添加一点精确信息: 在Python 3中,您应该将'SytemParametersInfoA'中的'A'替换为'W'。下面是一个小例子,用Python 3更改Windows10桌面背景:

import ctypes
import os
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(
    SPI_SETDESKWALLPAPER, 0, 'C:\\Users\\godet\\OneDrive\\Images\\breaker_wall.jpg', 0)

0

这对我有效

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20,0,path:os.PathLike,3)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接