2

I'm working on a simple (I thought) program to set a different desktop background for every day of the week. It runs with no errors but nothing happens. The path to the images are valid. Any ideas?

import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

localtime = time.localtime(time.time())
wkd = localtime[6]

if wkd == 6:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\1.jpg",0)

elif wkd == 0:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\2.jpg",0)

elif wkd == 1:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\3.jpg",0)

elif wkd == 2:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\4.jpg",0)

elif wkd == 3:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\5.jpg",0)

elif wkd == 4:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\6.jpg",0)

elif wkd == 5:
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,r"C:\Users\Owner\Documents\Wallpaper\7.jpg",0)
user3299467
  • 21
  • 1
  • 4

4 Answers4

4

I must have read every existing site on this topic and just before giving up, came to this working code (win7 pro 64 bit, python 3.4)

import ctypes
SPI_SETDESKWALLPAPER = 0x14     #which command (20)

SPIF_UPDATEINIFILE   = 0x2 #forces instant update
src = r"D:\Downloads\_wallpapers\3D-graphics_Line_025147_.jpg" #full file location
#in python 3.4 you have to add 'r' before "path\img.jpg"

print(ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, src, SPIF_UPDATEINIFILE))

It uses SystemParametersInfoW instead of SystemParametersInfoA (W instead of A).

Hope it helps you and many others who seem to have similar problems.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
qwasyx
  • 41
  • 2
2

If you are using Python 3 you should use ctypes.windll.user32.SystemParametersInfoW instead of ctypes.windll.user32.SystemParametersInfoA(W instead of A as this answer said). Another answer described that because in Python 3 the str type is in form of UTF-16 as wchar_t * in C.

What's more, please minimize the code like this:

import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

wallpapers = r"C:\Users\Owner\Documents\Wallpaper\%d.jpg"

localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, wallpapers%(wkd+1), 0)

Don't Repeat Yourself.

InQβ
  • 518
  • 4
  • 22
1

This is not an answer to your question, but you can generally minify your program and remove redundancy by doing something like this instead:

import time;
import ctypes;
SPI_SETDESKWALLPAPER = 20

wallpapers = [
    r"C:\Users\Owner\Documents\Wallpaper\1.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\2.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\3.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\4.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\5.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\6.jpg",
    r"C:\Users\Owner\Documents\Wallpaper\7.jpg",
]

localtime = time.localtime(time.time())
wkd = localtime[6]
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, wallpapers[wkd], 0)
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
0

I discovered a really weird reason on why my code wasn't working.

I had the function like this:

ctypes.windll.user32.SystemParametersInfoW(0x14, 0, """C:/Users/myUser/Desktop/
VSCoding/Python/TheWelcomer/test.jpg""", 0x2)

(There was an enter between the path)

and that didn't work, I just got a black desktop background

But when I got rid of that enter, like this:

ctypes.windll.user32.SystemParametersInfoW(0x14, 0, """C:/Users/myUser/Desktop/VSCoding/Python/TheWelcomer/test.jpg""", 0x2)

it worked

cigien
  • 57,834
  • 11
  • 73
  • 112
ZiyadCodes
  • 379
  • 3
  • 10