如何使用xrandr列出连接的监视器?

我正在为Ubuntu开发一个python应用程序,使用户能够在不需要图形驱动程序的情况下获得所需的分辨率。
为了做到这一点,我一直在使用xrandr ,到目前为止,它已经非常有用

但是,我现在有一个问题; 如何检测显示器名称? 我打算通过os.system使用终端命令,修改终端输出以获得所需的监视器输出,然后将其存储在程序中。 不幸的是,尽管经常搜索,但我一直无法知道如何做到这一点。

有什么方法可以做到这一点吗?

总结一下: 我正在寻找一个终端命令,它给我一个显示器名称,如VGA1DVI-0

我不确定你将如何在你的应用程序中应用它( “让用户在不需要图形驱动程序的情况下获得所需的分辨率” ?),但是:

列出已连接屏幕的终端命令

 xrandr | grep " connected " | awk '{ print$1 }' 

这将为您提供连接屏幕以进行进一步处理,例如:

 VGA-0 DVI-I-1 

既然你提到python,下面的代码片段也会列出连接的屏幕:

 #!/usr/bin/env python3 import subprocess def screens(): output = [l for l in subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()] return [l.split()[0] for l in output if " connected " in l] print(screens()) 

这将为您提供连接屏幕,如:

 ['VGA-0', 'DVI-I-1'] 

注意

请注意搜索字符串中" connected "周围的空格。 需要它们来防止与disconnected不匹配。

您可以使用pythonpython来获取连接的监视器名称:

 $ python3 -c 'from gi.repository import Gdk; screen=Gdk.Screen.get_default(); \ [print(screen.get_monitor_plug_name(i)) for i in range(screen.get_n_monitors())]' DP1 LVDS1 

您可以使用popen的bash命令:

 import os list_display = os.popen("xrandr --listmonitors | grep '*' | awk {'print $4'}").read().splitlines()