Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a process in Python and retrieving its window ID

Tags:

python

linux

Let's say I run:

import subprocess;
P = subprocess.Popen(['gnome-terminal');

I then get an object P of the class Popen:

<subprocess.Popen object at 0xb72a9d0c>

How can I then retrieve the window identity of this subprocess?

Ultimately, I would like to manipulate the geometrical properties of the window spawned by the subprocess using wmctrl, but for this I need something known as the windows identity of the window (see the documentation of wmctrl)

To provide more context to the question, wmctrl -l outputs a list of windows running on the GNOME desktop, where the first column shows the window identity of each window. This is a hexadecimal number that I have noticed is different from the one included in the output reported by Popen above.

I checked the Popen documentation but I am not sure how to proceed from this point.

Any thoughts? Thanks!

like image 414
Amelio Vazquez-Reina Avatar asked Oct 11 '22 09:10

Amelio Vazquez-Reina


1 Answers

An easy way to get the window ID is to use wmctrl -lp which will include the PID of the processes. Find the one that matches the Popen.pid of your subprocess and you'll find the window id of the process.

like image 85
nmichaels Avatar answered Oct 15 '22 10:10

nmichaels