Tel*_*Why 11 xrandr python command-line display resolution
我正在为 Ubuntu 开发一个 python 应用程序,它使用户无需图形驱动程序即可获得所需的分辨率。
为了做到这一点,我一直在使用xrandr
,到目前为止,它非常有用
但是,我现在有一个问题;如何检测监视器名称?我打算通过 使用终端命令os.system
,修改终端输出以获得所需的监视器输出,然后将其存储在程序中。不幸的是,尽管进行了多次搜索,但我一直无法找到如何做到这一点。
有什么办法可以做到这一点吗?
总结一下:我正在寻找一个终端命令,它给我监视器名称,例如VGA1
或DVI-0
Jac*_*ijm 15
我不确定您将如何在您的应用程序中应用它(“使用户无需图形驱动程序即可获得所需的分辨率”?),但是:
xrandr | grep " connected " | awk '{ print$1 }'
Run Code Online (Sandbox Code Playgroud)
这将为您提供连接的屏幕以供进一步处理,例如:
VGA-0
DVI-I-1
Run Code Online (Sandbox Code Playgroud)
VGA-0
DVI-I-1
Run Code Online (Sandbox Code Playgroud)
这也将为您提供连接的屏幕,例如:
['VGA-0', 'DVI-I-1']
Run Code Online (Sandbox Code Playgroud)
注意" connected "
搜索字符串中的空格。需要它们来防止与disconnected
.
使用 python,根本不需要使用xrandr
或任何其他系统调用。更好地使用 Gdk:
#!/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())
Run Code Online (Sandbox Code Playgroud)
示例输出:
[['eDP-1', 0, 0, 3840, 2160], ['DP-2', 3840, 562, 1680, 1050]]
Run Code Online (Sandbox Code Playgroud)
根据所需的信息,您可以从https://lazka.github.io/pgi-docs/Gdk-3.0/classes/Monitor.html进行选择
您可以使用带有 popen 的 bash 命令:
import os
list_display = os.popen("xrandr --listmonitors | grep '*' | awk {'print $4'}").read().splitlines()
# or based on the comment of this answer
list_display = os.popen("xrandr --listmonitors | grep '+' | awk {'print $4'}").read().splitlines()
Run Code Online (Sandbox Code Playgroud)
或者我写了一个关于这个主题的旧要点 https://gist.github.com/antoinebou13/7a212ccd84cc95e040b2dd0e14662445
您可以使用python
和python
来获取连接的监视器名称:
$ 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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18982 次 |
最近记录: |