![如果我连接特定的显示器,如何自动关闭默认显示器并设置分辨率?](https://linux22.com/image/1147243/%E5%A6%82%E6%9E%9C%E6%88%91%E8%BF%9E%E6%8E%A5%E7%89%B9%E5%AE%9A%E7%9A%84%E6%98%BE%E7%A4%BA%E5%99%A8%EF%BC%8C%E5%A6%82%E4%BD%95%E8%87%AA%E5%8A%A8%E5%85%B3%E9%97%AD%E9%BB%98%E8%AE%A4%E6%98%BE%E7%A4%BA%E5%99%A8%E5%B9%B6%E8%AE%BE%E7%BD%AE%E5%88%86%E8%BE%A8%E7%8E%87%EF%BC%9F.png)
每次我连接到特定的外部显示器时,默认分辨率都不是我想要的,所以我必须再次设置它。
如何为特定显示器保存特定分辨率,并在连接该显示器时关闭默认显示器?
答案1
根据所连接的(特定)屏幕设置分辨率
以下两个选项:
- 通过快捷键设置屏幕分辨率和切换屏幕(自动检测第二屏幕)
- 运行后台脚本自动关闭主屏幕并更改分辨率
选项 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
如何使用
- 将上述任一脚本复制到一个空文件中,并将其另存为
set_screens.py
在脚本的头部部分替换以下值:
# 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"
(当前设置仅适用于我的测试设置)
测试运行并应用脚本:
如果你使用选项 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"