How can you set the desktop background to a solid color programmatically in python?
The reason I want this is to make myself a utility which changes the background color depending on which of several virtual desktops I'm using.
CodePudding user response:
The correct way to do this is to call SystemParametersInfo
SPI_SETDESKWALLPAPER with an empty string to remove the wallpaper and then call SetSysColors
to change the desktop color. You probably need ctypes to do this in Python.
If you want to persist the change after log-off you also need to write the color to the registry.
CodePudding user response:
To change windows desktop wallpaper what we normally do is right click on the desktop and go properties and so on. But we can do the same by editing registry key using reg command from command line. The command is given below.
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d wallpaper_path /f
For example to set the image E:\photos\image1.bmp as the wall paper we need to run the command as below.
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d E:\photos\image1.bmp /f
After editing the registry key we need to run the below command to make the change take effect immediately.
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
Note
this method works only for bmp images. If you have .jpg or .jpeg images you can’t set them as wallpaper from command line. You can use the Desktop settings UI to set a .jpg or .jpeg file as wallpaper.
so now you can use os
or subprocess
to run cmd
then you can set desktop background
This page may also help you: How do I set the desktop background on Windows from a script?
Update
The background is stored in the registry. There are a few different ways to set it. But there several ways to set a registry value via the command line.
To set the background to a specific color you need to unset any wallpaers, and set the color.
HKEY_CURRENT_USER\Control Panel\Colors\Background = 'r g b'
HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper = ''