2

The following bit of python code is supposed to download an BMP image from the web and save it to disk, then change the wallpaper to the downloaded image. The wallpaper change is supposed to be permanent ie not revert back after restart. This function is part of a larger script that I am compiling into a binary exe using pyinstaller. The problem is that when I run the program the bit that is supposed to change the wallpaper is not working and I am at wits end trying to figure out why. The funny thing is if I run this exact code in the python interpreter it works as expected. Also, in a previous version of the compiled script, the wallpaper change worked without a hitch. Any comments, help, insights would be greatly appreciated!

def wallpaper():
    try:
        os.chdir(launch_directory)
        urllib.urlretrieve('http://www.imagehost.com/image.bmp', 'image.bmp')
        ctypes.windll.user32.SystemParametersInfoA(20, 0, os.path.join(launch_directory, "image.bmp"), 1)
    except:
        pass
2
  • Are you getting an exception? you are passing it silently it might be worth outputting any exception information available. Check the return type of SystemParametersInfo. If it is having trouble finding the image, that will tell you. It should return TRUE. Commented Oct 4, 2014 at 19:11
  • Try to change the last parameter of SystemParametersInfoA to 2 (SPIF_SENDCHANGE) Commented Oct 5, 2014 at 10:19

3 Answers 3

2

Change the "SystemParametersInfoA" to "SystemParametersInfoW". This fixed the desktop background update for me.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes, I am aware that I'm digging this thread from the 2014 (wow!), but having encountered the same problem, I've spent hours figuring out the solution and here it is: As .SystemParametersInfoW was leaving me with a black desktop, I've decided to stick with .SystemParametersInfoA but encode the path variable to us-ascii with a use of the cgi module. I wrote the following function which finally worked like a charm:

import ctypes as c
import cgi
def set_as_wallpaper(path): 
    SPI = 20
    SPIF = 2
    return c.windll.user32.SystemParametersInfoA(SPI, 0, path.encode("us-ascii"), SPIF)

Hope it helps!

Comments

1

For Linux refer below:-

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

For windows refer below:-

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

4 Comments

Thanks but I need this for windows only, not Linux.
It's the same as the code I've tried (refer to question) and it is not working. That's why I posted the question, I know what the code should be but it isn't working and I need help figuring out why.
The path is correct. os.path.join(launch_directory, "image.bmp") returns the absolute path to image.bmp which is residing in launch_directory. Even if I manually enter the absolute path still nothing. :(
@DarthCoder, if you're using Python 3, call windll.user32.SystemParametersInfoW instead. Make sure to use an an absolute path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.