5
votes

So I'm trying to make a Python 3.3 program to change the Windows desktop background using the ctypes module. I've tested the following code in Python 2.7, and it worked perfectly. But it just won't work with Python 3.3! I'm using Windows 7. Here's the code:

import ctypes
SPI_SETDESKTOPWALLPAPER=20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER, 0,"C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg", 3)
1
I'm attempting to figure out why I got voted down. I provided code and am merely asking for an explanation. There is nothing weak about the question. - Luke Dinkler
Forget downvotes, did you try SystemParametersInfoW - Bhargav Rao♦
Yes, it worked! Thanks! And sorry! I started my comment before you started yours. - Luke Dinkler
I'll accept your answer when it lets me. - Luke Dinkler
"I'm attempting to figure out why I got voted down" --> Tagging non-C posts as [C] attracts down votes. - chux - Reinstate Monica

1 Answers

10
votes

SystemParametersInfoA requires a 8-bit ANSI encoded input string as a parameter, which is known as mbcs encoding in Python.

You will have to use SystemParametersInfoW in python3. This is because SystemParametersInfoW takes in a UTF-16 wide string (which is wchar_t * in C) and the ctypes library automatically converts this passed unicode argument into c_wchar_p.

Refer the documentation for more details.