Skip to content
Advertisement

How to get which window manager is active using python?

I want to check which window manager is active using python? I used subprocess.run but it’s giving me string type output like below :

name: xfwm4
class: xfwm4
pid: 6981

I just want xfwm4 from name.Is there any alternative of subprocess and wmctrl for showing window manager? This is my code so far,

def getWM():
    try:      
        output = subprocess.run(['wmctrl', '-m'], text=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if output.stdout:
            s =  (output.stdout) + ' '
        return s
    except:
        return None

Advertisement

Answer

Using split is simplest:

import subprocess as sb
output=sb.run(['wmctrl', '-m'], text=True,stdout=sb.PIPE, stderr=sb.PIPE)
namestr=output.stdout.split('n')[0]
# For me this gives 'Name: KWin'
name=namestr.split(' ')[1]
# and this gives 'KWin'
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement