Note that there are some explanatory texts on larger screens.

plurals
  1. POGet display count and resolution for each display in Python without xrandr
    text
    copied!<p>I'm running Ubuntu and I want to get the number of attached displays, their current resolution and, if possible, their position in relation to each other. Because I don't like parsing Console output of xrandr - at least not if I don't have to - I would like to do that with <a href="http://python-xlib.sourceforge.net/" rel="noreferrer">Python-XLib</a> or a similar Pythonic approach.</p> <p>This is the xrandr output for my display config:</p> <pre><code>$ xrandr Screen 0: minimum 320 x 200, current 2960 x 1050, maximum 8192 x 8192 DVI-0 connected 1680x1050+0+0 (normal left inverted right x axis y axis) 473mm x 296mm 1680x1050 60.0*+ 1400x1050 60.0 1280x1024 75.0 60.0 1440x900 59.9 1280x960 75.0 60.0 1152x864 75.0 1280x720 75.0 1024x768 75.1 70.1 60.0 832x624 74.6 800x600 72.2 75.0 60.3 56.2 640x480 72.8 75.0 66.7 60.0 720x400 70.1 VGA-0 connected 1280x1024+1680+26 (normal left inverted right x axis y axis) 376mm x 301mm 1280x1024 60.0 + 75.0* 1024x768 75.1 70.1 60.0 832x624 74.6 800x600 72.2 75.0 60.3 56.2 640x480 72.8 75.0 66.7 60.0 720x400 70.1 </code></pre> <p>I want to get these values with Python, in a way like this:</p> <pre><code>displays = get_displays() print displays[0].width # out: 1680 print displays[1].width # out: 1280 print displays[0].x_position # out: 0 print displays[1].x_position # out: 1680 </code></pre> <p>When trying to get informations via Python-XLib (or other libs like pyGTK and pygame), it seems that all displays are always handled as one single display. For example this is what I got with XLib so far:</p> <pre><code>import Xlib import Xlib.display display = Xlib.display.Display(':0') print display.screen_count() # output: 1 root = display.screen().root print root.get_geometry().width # output: 2960 -&gt; no way to get width of single display? print root.get_geometry().height # output: 1050 </code></pre> <p>I know how to get display informations calling xrandr within Python:</p> <pre><code>import subprocess output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0] displays = output.strip().split('\n') for display in displays: values = display.split('x') width = values[0] height = values[1] print "Width:" + width + ",height:" + height </code></pre> <p>But as I said I would prefer a cleaner approach without having to parse Console output. Is there really no way to get (detailed) Display informations with Python without having to parse xrandr output?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload