如何使用PyGI通过Wnck获取窗口列表?
我刚开始在Ubuntu Natty上使用PyGI,之前从来没有用过pygtk。不过我在一个wxPython程序中用过wnck,那时候获取当前打开窗口的列表很简单。但在PyGI中,窗口列表总是空的。
相关的代码片段:
from gi.repository import Gtk, Wnck
while Gtk.events_pending():
Gtk.main_iteration()
#... in my app class...
screen = Wnck.Screen.get_default()
wins = screen.get_windows()
这样一来,wins == []
。
谢谢!
2 个回答
1
在你的例子中,你需要使用:Gtk.main_iteration_do(False)
,而不是 Gtk.main_iteration()
。
6
在你调用 screen.force_update()
之前,必须先执行这个方法,这样 screen.get_windows()
才能返回窗口的列表。可惜的是,文档里没有提到这一点 :(
In [1]: from gi.repository import Gtk, Wnck
In [2]: Gtk.main_iteration()
Out[2]: True
In [3]: screen = Wnck.Screen.get_default()
In [4]: screen.force_update()
In [5]: screen.get_windows()
Out[5]:
[<Window object at 0x167bd20 (WnckWindow at 0x195d0e0)>,
<Window object at 0x167bf00 (WnckWindow at 0x195d740)>,
<Window object at 0x167bf50 (WnckWindow at 0x195d850)>]