繁体   English   中英

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

[英]How Would You Change Windows Wallpaper Using Python & Hardware Detection?

我是 Python 的初学者,所以任何帮助将不胜感激,

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

我想创建一个脚本来执行以下操作:

  1. 检查2台显示器是否连接
  2. 如果他们在 Stretch 上将壁纸设置为“图像 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 中?

您可以通过以下步骤实现此目的。 首先你需要检测监视器计数(也许检查他们的名字) 看看这个答案

第二步将使用python中的os模块,调用os.system() function执行命令行命令来改变壁纸。 例如,这篇文章解释了如何从命令行更改 Windows 壁纸。

如果您好奇,这里是脚本:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM