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

2024-04-24 10:16:27 发布

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


Tags: python
3条回答

在python2.5或更高版本的Windows上,使用ctypes加载user32.dll,并使用SPI_SETDESKWALLPAPER操作调用^{}

例如:

import ctypes
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0)

对于Python3.5,系统参数sinfoa不起作用。使用SystemParametersInfoW。

import ctypes
ctypes.windll.user32.SystemParametersInfoW(20, 0, "absolute path" , 0)

在gnome桌面上,通常使用gconf来完成这项工作,可以直接调用gconftool,也可以使用gconf python模块。后者在联合国大学提供的链接中。第一种方法可以这样做。

import commands
command = "gconftool-2 --set /desktop/gnome/background/picture_filename --type string '/path/to/file.jpg'"
status, output = commands.getstatusoutput(command)  # status=0 if success

相关问题 更多 >