有没有办法在 Windows 上使用 python 更改显示器方向?

问题描述 投票:0回答:1

每次我转动显示器时,我都必须进入设置,有什么方法可以在Python中自动调整屏幕方向吗?我使用的是 Windows 10

我有 treid 使用快捷键(ctrl alt right),但这不起作用

这是gpt想出的东西

from screeninfo import get_monitors
import subprocess

def rotate_screen():
    # Get information about all monitors
    monitors = list(get_monitors())

    # Assuming the second monitor is the one you want to rotate
    if len(monitors) > 1:
        subprocess.run(["DisplaySwitch.exe", "/rotate:90"])

if __name__ == "__main__":
    rotate_screen()

显示开关用于选择如何将屏幕镜像到第二个屏幕

英语不是我的母语,如果有拼写错误,很抱歉

python windows multiple-monitors
1个回答
0
投票
However, there are a few adjustments you can make to Windows 10 to more consistently automate the screen orientation change. To adjust the screen orientation, you can use the pygetwindow and pyautogui libraries rather than the DisplaySwitch.exe command. 
This is an updated copy of your code:
pip install pygetwindow pyautogui # install the required libraries

import pygetwindow as gw
import pyautogui

def rotate_screen():
    # Find the window title of your screen orientation settings
    window_title = "Display Settings"

    # Activate the window
    if not gw.getWindowsWithTitle(window_title):
        print(f"Window with title '{window_title}' not found.")
        return

    # Assuming you want to rotate by 90 degrees
    pyautogui.hotkey('ctrl', 'alt', 'right')

if name == "main": 旋转屏幕()

© www.soinside.com 2019 - 2024. All rights reserved.