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
  • Try to change the last parameter of `SystemParametersInfoA` to `2` (`SPIF_SENDCHANGE`) – MC ND Oct 05 '14 at 10:19
  • 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. – Brad Oct 04 '14 at 19:11

3 Answers3

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)
Community
  • 1
  • 1
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Thanks but I need this for windows only, not Linux. – Darth Coder Oct 04 '14 at 19:05
  • 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. – Darth Coder Oct 04 '14 at 19:21
  • 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. :( – Darth Coder Oct 04 '14 at 19:44
  • @DarthCoder, if you're using Python 3, call `windll.user32.SystemParametersInfoW` instead. Make sure to use an an absolute path. – Eryk Sun Oct 05 '14 at 04:41
1

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

Nathan Clement
  • 1,103
  • 2
  • 18
  • 30
1

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!