質問

StackoverFlowが私の以前の質問に私のWiimoteの左/右クリックの問題について答えた後、マウスカーソルを移動できるだけでなく、左/右クリックを左/右クリックできます。もう1つ質問があります。

Pythonで何を使用して、現在のアクティブウィンドウのタイトルを取得しますか? 「X11 Python Windowタイトル」、「Linux Python Windowタイトル」などをグーグルで検索した後、私が見つけたのはWin32とTkinker(再び?)だけです。

あなたが助けることができれば、それは素晴らしいでしょう!

役に立ちましたか?

解決

編集

最良の方法:

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

他のヒント

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]

どのウィンドウがアクティブであるか、タイトルが何であるかを見つけるのは問題ですか?ウィンドウのタイトルを取得するのは簡単です:

MainWindow.title()

、MainWindowはウィンドウの名前です。しかし、アクティブウィンドウについてはわかりません。複数のウィンドウがありませんでした。

WindowsやWorkspacesなどを処理したい場合は、Python-WNCKが役立つと思います。すぐにPythonドキュメントを見つけることができませんが、Libwnck Cライブラリのドキュメントによると、それが包みます、それは wnck_screen_get_active_window() 方法。

私の解決策:

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()

WNCKには、アクティブウィンドウを更新するためにGTKイベントループが必要であることに気付きました。 Xlibにそのような問題はありません:

import Xlib
import Xlib.display
disp = Xlib.display.Display()
window = disp.get_input_focus().focus

# Get active window class and name
window.get_wm_class()
window.get_wm_name()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top