4

I'm working on developing a python application for Ubuntu that enables a user to have their desired resolution without requiring graphics drivers.
In order to do this I've been using xrandr, which so far, has been pretty useful

However, I now have a problem; how can I detect the monitor name? I was intending to use a terminal command through os.system, modifying the terminal output to get the required monitor output, and then storing it within the program. Unfortunately, despite much searching, I have been unable to find out how I can do this.

Is there any way in which I can do this?

To sum it up: I'm looking for a terminal command that gives me the monitor name, such VGA1 or DVI-0

4

I am not sure how you are going to apply it in your application ("enable a user to have their desired resolution without requiring graphics drivers" ?), but:

A terminal command to list connected screens

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

This wil give you the connected screens for further processing, like:

VGA-0
DVI-I-1

Since you mention python, the snippet below will also list connected screens:

#!/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())

This wil also give you the connected screens, like:

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

Note

Note the spaces around " connected " in the searched string. They are needed to prevent mismatches with disconnected.

4

You can use python and just python to get the connected monitor names:

$ 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
  • Nice! didn't know this one. – Jacob Vlijm Jun 22 '15 at 19:10
1

You can use the bash command with popen:

import os
    list_display = os.popen("xrandr --listmonitors | grep '*' | awk {'print $4'}").read().splitlines()
  • 1
    How does this answer the question? – Stephen Rauch Jun 24 '18 at 4:20
  • +1 for using xrandr --listmonitors :) – simon Jan 26 at 2:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.