您将如何使用Python和硬件检测更改Windows墙纸?

2024-06-11 17:15:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者,非常感谢您的帮助

我有一台我使用的Windows笔记本电脑和两台我连接到它的显示器,当我使用显示器时,笔记本电脑盖是关闭的,当我使用笔记本电脑时,我远离显示器

我想创建一个脚本,该脚本将执行以下操作:

  1. 检查两个监视器是否已连接
  2. 如果是,则在拉伸时将壁纸设置为“图像1”
  3. 如果只连接了笔记本电脑屏幕,则在填充时将壁纸设置为“图像2”

我希望将其转换为代码:

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

有人能给我一些建议吗?如果不是用Python,我会怎么做


Tags: to图像image脚本pngscreen显示器users
2条回答

如果您好奇,以下是脚本:

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)

您可以通过以下步骤实现这一点。首先,您需要检测监视器计数(并可能检查其名称)look at this answer

第二步将使用python中的os模块,调用os.system()函数执行命令行命令以更改墙纸。例如this post说明如何从命令行更改Windows墙纸

相关问题 更多 >