Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get window position and size in python with Xlib

Tags:

python

xlib

I need to find window position and size, but I cannot figure out how. For example if I try:

id.get_geometry()    # "id" is Xlib.display.Window

I get something like this:

data = {'height': 2540,
'width': 1440,
'depth': 24,
'y': 0, 'x': 0,
'border_width': 0
'root': <Xlib.display.Window 0x0000026a>
'sequence_number': 63}

I need to find window position and size, so my problem is: "y", "x" and "border_width" are always 0; even worse, "height" and "width" are returned without window frame.

In this case on my X screen (its dimensions are 4400x2560) I expected x=1280, y=0, width=1440, height=2560.

In other words I'm looking for python equivalent for:

#!/bin/bash
id=$1
wmiface framePosition $id
wmiface frameSize $id

If you think Xlib is not what I want, feel free to offer non-Xlib solution in python if it can take window id as argument (like the bash script above). Obvious workaround to use output of the bash script in python code does not feel right.

like image 517
Lissanro Rayen Avatar asked Oct 08 '12 04:10

Lissanro Rayen


People also ask

How do I get the Windows position in Python?

You can get the window coordinates using the GetWindowRect function. For this, you need a handle to the window, which you can get using FindWindow , assuming you know something about the window (such as its title). To call Win32 API functions from Python, use pywin32 .

What is Python xlib?

Show activity on this post. "Python Xlib" ( http://pypi.python.org/pypi/Python%20Xlib ) is a low level python library for working with xlib. I have installed it on my Ubuntu Linux machine via apt, i.e. sudo aptitude install python-xlib .


1 Answers

You are probably using reparenting window manager, and because of this id window has zero x and y. Check coordinates of parent window (which is window manager frame)

like image 114
Andrey Sidorov Avatar answered Oct 05 '22 23:10

Andrey Sidorov