4

在 Ubuntu Linux / Gnome 上,我正在尝试使用 python 的 wnck 绑定来获取所有打开的窗口的列表。

我的代码如下所示:

#! /usr/bin/python

from pprint import pprint
import wnck

screen = wnck.screen_get_default()

pprint(screen.get_windows())

import pdb ; pdb.set_trace()

pprint(screen.get_windows())

输出是这样的:

** (windowlist.py:4501): WARNING **: Trying to register gtype 'WnckWindowState' as enum when in fact it is of type 'GFlags'

** (windowlist.py:4501): WARNING **: Trying to register gtype 'WnckWindowActions' as enum when in fact it is of type 'GFlags'

** (windowlist.py:4501): WARNING **: Trying to register gtype 'WnckWindowMoveResizeMask' as enum when in fact it is of type 'GFlags'
[]
> /home/rory/personal/diriu/windowlist.py(12)<module>()
-> pprint(screen.get_windows())
(Pdb) c
[<wnck.Window object at 0xb61db0cc (WnckWindow at 0x992c000)>,
 <wnck.Window object at 0xb61db0f4 (WnckWindow at 0x992c0a8)>,
 <wnck.Window object at 0xb61db11c (WnckWindow at 0x992c150)>,
 <wnck.Window object at 0xb61db144 (WnckWindow at 0x992c1f8)>,
 <wnck.Window object at 0xb61db16c (WnckWindow at 0x992c2a0)>,
 <wnck.Window object at 0xb61db194 (WnckWindow at 0x992c348)>,
 <wnck.Window object at 0xb61db1bc (WnckWindow at 0x992c3f0)>,
 <wnck.Window object at 0xb61db1e4 (WnckWindow at 0x992c498)>,
 <wnck.Window object at 0xb61db20c (WnckWindow at 0x992c540)>,
 <wnck.Window object at 0xb61db234 (WnckWindow at 0x992c5e8)>,
 <wnck.Window object at 0xb61db25c (WnckWindow at 0x992c690)>,
 <wnck.Window object at 0xb61db284 (WnckWindow at 0x992c738)>]

第一次get_windows()调用返回[]一个空列表。第二个get_windows()调用返回打开的窗口列表。在 中pdb,我只按c继续,什么也不做(在 pdb 中)。

如果我注释掉pdb,这两个get_windows()调用都会返回[]。如果我get_windows()在 pdb 之前多次重复调用,则什么都没有。如果我get_windows()在 pdb 之后多次重复调用,我会不断得到一个完整的列表。

为什么这个调用只在一个空的 pdb 之后才起作用?如何在没有 pdb 的情况下使其工作?

4

2 回答 2

1

文件 so-wnck.py (来自我如何抓取标题中带有特定单词的所有窗口?

import pygtk
pygtk.require('2.0')
import gtk
import wnck

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

windows = screen.get_windows()
print (windows)

python so-wnck.py

** (so-wnck.py:2367): WARNING **: Trying to register gtype 'WnckWindowState' as enum when in fact it is of type 'GFlags'

** (so-wnck.py:2367): WARNING **: Trying to register gtype 'WnckWindowActions' as enum when in fact it is of type 'GFlags'

** (so-wnck.py:2367): WARNING **: Trying to register gtype 'WnckWindowMoveResizeMask' as enum when in fact it is of type 'GFlags'
[<wnck.Window object at 0xb7566694 (WnckWindow at 0x97c51f8)>,
 <wnck.Window object at 0xb75666bc (WnckWindow at 0x97c52a0)>, 
 <wnck.Window object at 0xb75666e4 (WnckWindow at 0x97c5348)>, 
 <wnck.Window object at 0xb756670c (WnckWindow at 0x97c53f0)>, 
 <wnck.Window object at 0xb7566734 (WnckWindow at 0x97c5498)>,
 <wnck.Window object at 0xb756675c (WnckWindow at 0x97c5540)>, 
 <wnck.Window object at 0xb7566784 (WnckWindow at 0x97c55e8)>]
于 2011-03-19T20:08:46.660 回答
0

我不使用 wnck 但我认为如果两次调用 get_window 之间的 pdb 时间足以初始化 Windows 列表。在调用 get_window 之前尝试使用睡眠稍有延迟。

于 2011-03-18T13:22:24.117 回答