14

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 Answers 4

19

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.

EDIT 2019

Using python, not necessary to use xrandr or any other system call at all. Better use Gdk:

#!/usr/bin/env python3
import gi
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk

allmonitors = []

gdkdsp = Gdk.Display.get_default()
for i in range(gdkdsp.get_n_monitors()):
    monitor = gdkdsp.get_monitor(i)
    scale = monitor.get_scale_factor()
    geo = monitor.get_geometry()
    allmonitors.append([
        monitor.get_model()] + [n * scale for n in [
            geo.x, geo.y, geo.width, geo.height
        ]
    ])

print(allmonitors)

Example output:

[['eDP-1', 0, 0, 3840, 2160], ['DP-2', 3840, 562, 1680, 1050]]

Depending on the desired info, you can make your choice from https://lazka.github.io/pgi-docs/Gdk-3.0/classes/Monitor.html

1
  • Note that the Python solution only lists displays that have not been disabled via xrandr.
    – wil
    Apr 4, 2022 at 19:36
8

You can use the bash command with popen:

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()
xrandr --listmonitors | grep '*' | awk {'print $4'}
xrandr --listmonitors | grep '+' | awk {'print $4'}

or I wrote a old gist on the subject https://gist.github.com/antoinebou12/7a212ccd84cc95e040b2dd0e14662445

4
  • 2
    How does this answer the question? Jun 24, 2018 at 4:20
  • 5
    +1 for using xrandr --listmonitors :)
    – simon
    Jan 26, 2019 at 2:04
  • @simon what does the + sign in the output mean. I saw a monitor that do not have a + sign
    – doraemon
    Apr 3, 2022 at 8:36
  • 1
    Use a shell, not python for this. os.popen is discouraged as well. Oct 10, 2022 at 0:37
6

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
2
  • 1
    Nice! didn't know this one. Jun 22, 2015 at 19:10
  • I get a couple of warnings: "<string>:2: DeprecationWarning: Gdk.Screen.get_n_monitors is deprecated <string>:2: DeprecationWarning: Gdk.Screen.get_monitor_plug_name is deprecated" in Xubuntu 20.04
    – jarno
    Dec 25, 2020 at 16:23
0

Since I can't add comments, I have to add an complete answer. Sorry for that.

A terminal command to list connected screens

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

Just a small adjustment:

awk '/\<connected\>/ {print $1}'< <(xrandr)

or

xrandr | awk '/\<connected\>/ {print $1}'

awk searches for the pattern within the forward slashes / and prints the first column.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .