如果我连接特定的显示器,如何自动关闭默认显示器并设置分辨率?

如果我连接特定的显示器,如何自动关闭默认显示器并设置分辨率?

每次我连接到特定的外部显示器时,默认分辨率都不是我想要的,所以我必须再次设置它。

如何为特定显示器保存特定分辨率,并在连接该显示器时关闭默认显示器?

答案1

根据所连接的(特定)屏幕设置分辨率

以下两个选项:

  1. 通过快捷键设置屏幕分辨率和切换屏幕(自动检测第二屏幕)
  2. 运行后台脚本自动关闭主屏幕并更改分辨率

选项 1;快捷方式

#!/usr/bin/env python3
import subprocess
import time

# set the default screen
default = "DVI-I-1"
# set the specific external screen
external = "VGA-1"
# set the resolution of the single screen setup
singleres = "1680x1050"
# set the resolution of the specific external screeen
extrares = "1280x1024"

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8")

def run(cmd):
    subprocess.call(cmd)

def get_screens():
    return [l.split()[0] for l in get("xrandr").splitlines() if " connected" in l]

def set_screen(n_scr, screens):
    if n_scr == 1:
        run(["xrandr", "--output", default, "--auto"])
        run(["xrandr", "-s", singleres])
        print("1 screen")
    elif all([n_scr == 2, external in screens]):    
        run(["xrandr", "--output", default, "--off"])
        run(["xrandr", "-s", extrares])
        print("2 screens")

screens = get_screens()
n_scr2 = len(screens)
set_screen(n_scr2, screens)

选项 2;背景版本

#!/usr/bin/env python3
import subprocess
import time

# set the default screen
default = "DVI-I-1"
# set the specific external screen
external = "VGA-1"
# set the resolution of the single screen setup
singleres = "1680x1050"
# set the resolution of the specific external screeen
extrares = "1280x1024"

def get(cmd):
    try:
        return subprocess.check_output(cmd).decode("utf-8")
    except subprocess.CalledProcessError:
        pass

def run(cmd):
    subprocess.call(cmd)

def get_screens(scrdata):
    return [l.split()[0] for l in scrdata.splitlines() if " connected" in l]

def set_screen(n_scr, screens):
    if n_scr == 1:
        run(["xrandr", "--output", default, "--auto"])
        run(["xrandr", "-s", singleres])
        print("1 screen")
    elif all([n_scr == 2, external in screens]):    
        run(["xrandr", "--output", default, "--off"])
        run(["xrandr", "-s", extrares])
        print("2 screens")

n_scr1 = None

while True:
    time.sleep(4)
    scrdata = get("xrandr")
    if scrdata:
        screens = get_screens(scrdata)
        n_scr2 = len(screens)
        if n_scr2 != n_scr1:
            set_screen(n_scr2, screens)
        n_scr1 = n_scr2

如何使用

  1. 将上述任一脚本复制到一个空文件中,并将其另存为set_screens.py
  2. 在脚本的头部部分替换以下值:

    # set the default screen
    default = "DVI-I-1"
    # set the specific external screen
    external = "VGA-1"
    # set the resolution of the single screen setup
    singleres = "1680x1050"
    # set the resolution of the specific external screeen
    extrares = "1280x1024"
    

    (当前设置仅适用于我的测试设置)

  3. 测试运行并应用脚本:

    • 如果你使用选项 1,快捷方式

      打开终端,依次使用和不使用外部屏幕运行脚本,命令如下:

      python3 /path/to/set_screens.py
      

      它应该按预期设置屏幕。

      随后,如果一切正常,将脚本添加到快捷方式:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:

      python3 /path/to/set_screens.py
      
    • 如果你使用选项 2,后台脚本

      打开终端,使用以下命令运行脚本:

      python3 /path/to/set_screens.py
      

      并连接/断开外接显示器。它应该会按预期更改分辨率并打开/关闭默认显示器。

      随后,如果一切正常,将脚本添加到启动应用程序:Dash > 启动应用程序 > 添加。添加命令:

      /bin/bash -c "sleep 10 && python3 /path/to/set_screens.py"
      

相关内容