Name Xorg, Python and Current Window

After stackoverflow answered my previous question about my Wiimote left / right click problem, I can not only move the mouse cursor, but now I can leave / right-click on things. I now have one more question.

What am I using in python to get the title of the current active window? After googling “X11 Python Window Title”, “Linux Python Window Title” and similar things, all I found was win32 and tkinker (again?), Which is not what I need.

If you could help, that would be awesome!

+3
source share
5 answers

EDIT

The best way:

import gtk
import wnck
import glib

class WindowTitle(object):
    def __init__(self):
        self.title = None
        glib.timeout_add(100, self.get_title)

    def get_title(self):
        try:
            title = wnck.screen_get_default().get_active_window().get_name()
            if self.title != title:
                self.title  = title
                print title
        except AttributeError:
            pass
        return True

WindowTitle()
gtk.main()

:

from subprocess import PIPE, Popen
import time

title = ''
root_check = ''

while True:
    time.sleep(0.6)
    root = Popen(['xprop', '-root'],  stdout=PIPE)

    if root.stdout != root_check:
        root_check = root.stdout

        for i in root.stdout:
            if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
                id_ = i.split()[4]
                id_w = Popen(['xprop', '-id', id_], stdout=PIPE)

        for j in id_w.stdout:
            if 'WM_ICON_NAME(STRING)' in j:
                if title != j.split()[2]:
                    title = j.split()[2]
                    print "current window title: %s" % title
+12

killown xprop ( ) :

import subprocess
def GetActiveWindowTitle():
    return subprocess.Popen(["xprop", "-id", subprocess.Popen(["xprop", "-root", "_NET_ACTIVE_WINDOW"], stdout=subprocess.PIPE).communicate()[0].strip().split()[-1], "WM_NAME"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip().split('"', 1)[-1][:-1]
+4

, , ? :

MainWindow.title()

MainWindow - . . .

0

, python-wnck , Windows .. Python, libwnck C, , wnck_screen_get_active_window() .

0

:

import wnck
disp=Display()
default_screen=wnck.screen_get_default()
default_screen.force_update()

active_window=disp.create_resource_object('window', default_screen.get_active_window().get_xid())
title=active_window.get_wm_name()
0

Source: https://habr.com/ru/post/1761195/


All Articles