Python的模塊pywin32中的win32gui.SystemParametersInfo()函數api
在使用win32con.SPI_SETDESKWALLPAPER設置Wallpaper時,其第二個參數爲圖片路徑,圖片必須是BMP格式。以下:函數
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imagepath, 1+2)ui
不然將報錯以下:spa
pywintypes.error: (0, 'SystemParametersInfo', 'No error message is available')code
在Python中設置桌面壁紙的方法以下: blog
首先須要 import win32api, win32gui, win32api, Image圖片
而後經過如下兩個函數實現:class
1 def setWallpaperFromBMP(imagepath): 2 k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE) 3 win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸適應桌面,0桌面居中 4 win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0") 5 win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2) 6 7 # convert jpg to bmp 8 def setWallPaper(imagePath): 9 bmpImage = Image.open(imagePath) 10 newPath = imagePath.replace('.jpg', '.bmp') 11 bmpImage.save(newPath, "BMP") 12 setWallpaperFromBMP(newPath)