1

This has been solved by erykson:

import os, ctypes
SPI_SETDESKWALLPAPER = 20
SPIF_UPDATEINIFILE = 1
SPIF_SENDCHANGE = 2
image_path = os.path.abspath("image.jpg")
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)

Here's a single line version

import os, ctypes
ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.abspath("image.jpg"), 1 | 2)

I would like to begin by stating that using the ctypes method has been unsuccessful for me and calling a powershell function has also been unsuccessful for me.

The goal of my program is to change the desktop background in real time when the program runs. I've been unsuccessful in my attempts and I am relatively new to doing such a task. If anyone can guide me with a nice bit of code and a little explanation on how it works, that would be most appreciated. Again, this is for Windows 8.1 64bit and I am using Python 3.4.3 64bit. I have jpg files, but I can use any format as long as it works. Thank you for any possible solutions or for a semi-detailed description on why this won't work.

PowerShell Function Code

Function Set-WallPaper($Value)
{
 Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
 rundll32.exe user32.dll, UpdatePerUserSystemParameters
}

PowerShell Call Code in Python

import subprocess
subprocess.Popen([r'path/to/PowerShell/script.exe',
                     '-ExecutionPolicy',
                     'Unrestricted',
                     'path/to/background_image.png'], cwd=os.getcwd())

and then I've tried this with ctypes, shows a black screen

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

Before anyone asks, yes I changed the directories when implementing these lines.

Community
  • 1
  • 1
Maskot
  • 13
  • 4
  • 1
    Please show us the code you have attempted? – Vaibhav Mule Jun 06 '15 at 02:19
  • Python 3 uses Unicode strings, so call `SystemParametersInfoW`. You should also update the registry and broadcast the change by passing `SPIF_UPDATEINIFILE | SPIF_SENDCHANGE` in `fWinIni` (4th argument). – Eryk Sun Jun 06 '15 at 03:59
  • I have less than 6 months experience with coding, so I'm not really picking up what you put down. – Maskot Jun 06 '15 at 04:11
  • `import os, ctypes;` `SPI_SETDESKWALLPAPER = 20;` `SPIF_UPDATEINIFILE = 1;` `SPIF_SENDCHANGE = 2;` `image_path = os.path.abspath("image.jpg");` `success = ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)` – Eryk Sun Jun 06 '15 at 04:21

0 Answers0