1

I have a python script that should technically change my wallpaper to a downloaded .jpg file. While the code executes without any errors, my wallpaper changes to black color with no image. I am not sure whether I made a mistake, or there is a different approach that will work. Thanks for looking, Cheers!

I am running Windows Version of Python(v3.6.0a4)

import ctypes
import urllib
import os 
import urllib.request


SPI_SETDESKWALLPAPER = 20 
f = open('env.jpg','wb')
f.write(urllib.request.urlopen('http://i68.tinypic.com/vzhlrp.jpg').read())
path = os.getcwd() + "\env.jpg"

print("\n" + path)


ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path , 0)

I also tried changing the path to 'C:\env.jpg', it does not work also.

EDIT Also tried a more stable version of python, v3.5.1, still does not work

EDIT 2 Finally figured it out, I needed to add f.close() to close stream.

import ctypes
import urllib
import os 
import urllib.request


SPI_SETDESKWALLPAPER = 20 
f = open('env.jpg','wb')
f.write(urllib.request.urlopen('http://i68.tinypic.com/vzhlrp.jpg').read())
f.close() #THIS LINE MADE ALL THE DIFFERENCE

path = os.path.join(os.getcwd(), "env.jpg")

print("\n" + path)



ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path , 0)
Mike
  • 11
  • 2
  • Yes, its the windows embeddable zip version. – Mike Aug 18 '16 at 22:27
  • Ah... I was thinking the alpa version released 2 days ago. – double_j Aug 18 '16 at 22:27
  • Would you happen to know the solution? haha – Mike Aug 18 '16 at 22:28
  • Would you mind changing `path = os.getcwd() + "\env.jpg"` to `path = os.path.join(os.getcwd(), "env.jpg")` and seeing if that does anything? – TigerhawkT3 Aug 18 '16 at 22:30
  • Same result, just sets a black screen without wallpaper. – Mike Aug 18 '16 at 22:34
  • Try changing `SystemParametersInfoA` to `SystemParametersInfoW`. – TigerhawkT3 Aug 18 '16 at 22:48
  • Problem persists, sadly. Would you be able to test this code out on your machine? – Mike Aug 18 '16 at 22:52
  • Also if I keep both InfoA and InfoW lines, when I press WINKEY + D to go to desktop, the 'env.jpg' flashes for split second, and then wallpaper goes black. EDIT just stopped working again – Mike Aug 18 '16 at 22:54
  • Does anything [here](http://stackoverflow.com/questions/21715895/creating-a-background-changer-in-python-with-ctypes-not-working) work? – TigerhawkT3 Aug 18 '16 at 23:02
  • Ive pinpointed the problem. It is a problem with the file download, the script will not set a downloaded image as wallpaper. If I put any image from my laptop to os.getcwd() then script works, but if the script downloads the image to os.getcwd() then it does not work. – Mike Aug 19 '16 at 00:04
  • SOLVED WITH F.CLOSE() - THANK YOU FOR ALL THE HELP!! – Mike Aug 19 '16 at 00:17

0 Answers0