Windows 用户在这里,跳上 Ubuntu 火车。目前使用14.04. 我有一台用于观看电影等的电视,通过 HDMI 连接。我想完成以下行为,但我还没有弄清楚
只有修改设置我才能实现,但它非常手动。我错过了什么吗?
非常感谢!
这可以通过运行一个小的后台脚本(在启动(登录)时自动启动)轻松完成。
如果您的电视(或任何其他辅助屏幕)已连接,脚本将自动关闭您的内部屏幕。如果您只希望它适用于特定的外部屏幕,请提及。
#!/usr/bin/env python3
import subprocess
import time
# --- set your internal screen below (the example is my primary screen)
internal = "DVI-I-1"
#---
# don't change anything below
scr_info1 = 0
while True:
time.sleep(4)
# read the current screen setup from xrandr
get_screens = subprocess.check_output("xrandr").decode("utf-8").splitlines()
scr_data = [l for l in get_screens if " connected " in l]
# count the number of connected screens
scr_info2 = len(scr_data)
# if the number of connected screens changes,
# switch off the internal one if there are two screens
if scr_info2 != scr_info1:
if scr_info2 == 2:
ext = [s.split()[0] for s in scr_data if not internal in s][0]
subprocess.Popen(["xrandr", "--output", internal, "--off", "--output", ext, "--auto"])
scr_info1 = scr_info2
Run Code Online (Sandbox Code Playgroud)
switch_screens.pyxrandr(未连接外部屏幕)其中带有“已连接”的行在第一个字符串中显示屏幕名称,看起来像:VGA-1或类似的内容。通过打开终端窗口并键入命令来测试运行它:
python3 /path/to/switch_screens.py
Run Code Online (Sandbox Code Playgroud)
在脚本运行时,连接您的电视,等待您的内部屏幕切换并再次断开连接。
如果一切正常,请将以下命令添加到启动应用程序:打开 Dash > 启动应用程序 > 添加。添加命令:
/bin/bash -c "sleep 15 && python3 /path/to/switch_screens.py"
Run Code Online (Sandbox Code Playgroud)注销并重新登录。现在,如果连接了外部屏幕,您的内部屏幕会自动关闭。