0

I'm a beginner to Python so any help will be much appreciated,

I have a Windows laptop which I use and a 2 monitors which I connect to it, when I am using the monitors the laptop lid is shut, and when I'm using the laptop I'm away from the monitors

I want to create a script that will do the following:

  1. Check whether the 2 monitors are connected
  2. If they are then set the wallpaper to "Image 1" on Stretch
  3. If the laptop screen is only connected, then set the wallpaper to "Image 2" on Fill

I would want to turn this into code essentially:

Image 1 = "C:\\Users....\Image1.png"
Image 2 = "C:\\Users....\Image2.png"

Laptop Screen = HP ENVY x360 Screen?
Dual Monitors = SAMSUNG 24 and SAMSUNG 24

if Dual Monitors is connected:
     set wallpaper to Image 2 on Stretch
else:
     set wallpaper to Image 1 on Fill

Can someone advise me on how I would do this but in Python?

Mofi
  • 46,139
  • 17
  • 80
  • 143
f14ce
  • 1

2 Answers2

0

You can achieve this with following steps. First you need to detect monitor count (and maybe check their names) look at this answer

Second step will be using os module in python, call os.system() function to execute command line command to change wallpaper. For example this post explains how to change Windows wallpaper from command line.

  • Hi, Thanks for that I managed to put a script together All I now need is a way to run this constantly in the background to check whether the monitors have changed. Do you have any idea on how to do this? Note: I'm using Windows 10 – f14ce Feb 12 '21 at 09:28
0

Here is the script if you are curious:

import ctypes
import subprocess
import re

# Wallpapers And Their Paths
Image1 = r"C:\Users\user\Documents\People\Farzanul Chowdhury\Python\Automatic Wallpaper\Images\1.png"
Image2 = r"C:\Users\user\Documents\People\Farzanul Chowdhury\Python\Automatic Wallpaper\Images\2.png"

# Get The Name of The Montiors Active
proc = subprocess.Popen(['powershell', 'Get-WmiObject win32_desktopmonitor;'], stdout=subprocess.PIPE)
res = proc.communicate()
monitor = re.findall('(?s)\r\nName\s+:\s(.*?)\r\n', res[0].decode("utf-8"))
AvailableMonitor = f"z{monitor}z"

# Set The Wallpaper
if AvailableMonitor == "z['Generic PnP Monitor']z":
    ctypes.windll.user32.SystemParametersInfoW(20, 0 , Image1, 0)
else:
    ctypes.windll.user32.SystemParametersInfoW(20, 0 , Image2, 0)
f14ce
  • 1