PythonのXlibを使用してXウィンドウを検索する

PythonのXlibを使用してXウィンドウを検索する

私のXサーバーでマップされていないウィンドウを見つけて再マップし、いくつかを送信しようとしています。ヨーロッパWMHヒント。ウィンドウがマップされていないため、EWMHを使用してウィンドウマネージャに直接連絡することはできません。だから私はXlibを介して取得しようとしましたが、問題が発生しました。 API全体が私にとってとても混乱しています。

私はPythonを使用しています。Xlibラッパー。それでは、次のPythonスクリプトを見てみましょう。

import subprocess
from time import sleep
from ewmh import EWMH

subprocess.Popen(['urxvt']) # Run some program, here it is URXVT terminal.
sleep(1) # Wait until the term is ready, 1 second is really enought time.

ewmh = EWMH() # Python has a lib for EWMH, I use it for simplicity here.

# Get all windows?
windows = ewmh.display.screen().root.query_tree().children

# Print WM_CLASS properties of all windows.
for w in windows: print(w.get_wm_class())

スクリプトの出力は何ですか?開いているURXVT端末は次のとおりです。

None
None
None
None
('xscreensaver', 'XScreenSaver')
('firefox', 'Firefox')
('Toplevel', 'Firefox')
None
('Popup', 'Firefox')
None
('Popup', 'Firefox')
('VIM', 'Vim_xterm')

しかし、このコマンドを実行して開いている端末をクリックすると、次のようになります。

$ xprop | grep WM_CLASS
WM_CLASS(STRING) = "urxvt", "URxvt"

と同じWM_NAME財産。

最後の質問:スクリプト出力に「URxvt」文字列がないのはなぜですか?

ベストアンサー1

文字列がない理由「urxvt」、「URxvt」XWindowsは階層的です。何らかの理由で、私のデスクトップでは、urxvtウィンドウは最初のレベルにありません。

したがって、次のようにツリー全体をナビゲートする必要があります。

from Xlib.display import Display

def printWindowHierrarchy(window, indent):
    children = window.query_tree().children
    for w in children:
        print(indent, w.get_wm_class())
        printWindowHierrarchy(w, indent+'-')

display = Display()
root = display.screen().root
printWindowHierrarchy(root, '-')

スクリプト出力の1行(非常に長い)は次のとおりです。

--- ('urxvt', 'URxvt')

おすすめ記事