Discover what window is active on Gnome/Linux/Ubuntu from Python?

六眼飞鱼酱① 提交于 2020-01-01 03:31:38

问题


Is there any way to get a list of all windows that are open at present and see what window is at the top (i.e. active?) from Python?

This is using Gnome on Ubuntu Linux.

wnck looks like it might do this, but it's very lacking in documentation.


回答1:


import wnck
screen = wnck.screen_get_default()
window_list = screen.get_windows()
active_window = screen.get_active_window()

See also Get active window title in X, and WnckScreen in the documentation. Other questions containing wnck have useful code samples.




回答2:


Here's the same code using the modern GObject Introspection libraries instead of the now deprecated PyGTK method Josh Lee posted:

from gi.repository import Gtk, Wnck

Gtk.init([])  # necessary if not using a Gtk.main() loop
screen = Wnck.Screen.get_default()
screen.force_update()  # recommended per Wnck documentation

window_list = screen.get_windows()
active_window = screen.get_active_window()

As for documentation, check out the Libwnck Reference Manual. It is not specific for python, but the whole point of using GObject Introspection is to have the same API across all languages, thanks to the gir bindings.

Also, Ubuntu ships with both wnck and its corresponding gir binding out of the box, but if you need to install them:

sudo apt-get install libwnck-3-* gir1.2-wnck-3.0

This will also install libwnck-3-dev, which is not necessary but will install useful documentation you can read using DevHelp



来源:https://stackoverflow.com/questions/4935863/discover-what-window-is-active-on-gnome-linux-ubuntu-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!