简体   繁体   English

如何使用pygtk获取gnome2桌面上所有窗口的列表?

[英]How do I get a list of all windows on my gnome2 desktop using pygtk?

I'm a bit confused with some gtk and gnome concepts. 我对一些gtk和gnome概念感到困惑。 I'm trying to get list of non minimized windows on my gnome2 desktop, but after reading the pygtk documentation and inspecting the results, I can't understand the results. 我正在尝试获取gnome2桌面上非最小化窗口的列表,但在阅读了pygtk文档并检查结果后,我无法理解结果。

Neither of the two snippets below appears to work. 下面的两个片段似乎都不起作用。

First I tried this.. 首先我试过这个..

>>> gtk.gdk.window_get_toplevels()
[<gtk.gdk.Window object at 0xb74339b4 (GdkWindow at 0x8a4c170)>]

>>> gtk.gdk.window_get_toplevels()[0].get_children()
[]

then this 那么这个

>>> d = gtk.gdk.DisplayManager()   
>>> d.get_default_display().get_screen(0).get_root_window().get_children() 
[<gtk.gdk.Window object at 0x89dcc84 (GdkWindow at 0x8a4c170)>, <gtk.gdk.Window object at 0x89dccac (GdkWindow at 0x8a4c0c0)>] 

As seen in the console output, the second option returns two windows. 如控制台输出中所示,第二个选项返回两个窗口。 But I haven't been able to figure out what they are. 但我无法弄清楚它们是什么。 None of them has any children and I allways get those two windows regardless how many windows I have on my desktop. 他们都没有任何孩子,无论我在桌面上有多少个窗口,我总是得到这两个窗口。

Could anybody explain the hierarchy of objects of the typical gtk based desktop environment? 任何人都可以解释典型的基于gtk的桌面环境的对象层次结构吗? I can't understand why the above code doesn't work. 我无法理解为什么上面的代码不起作用。

Please refrain from posting alternative solutions that resource to wnck, xlib, qt, etc. I'm more interested in understanding what I am doing wrong than in getting advice such us checking other libraries. 请不要发布资源到wnck,xlib,qt等的替代解决方案。我更感兴趣的是了解我做错了什么,而不是获取建议,比如我们检查其他库。

Your constraint is like saying "I want to build a CD player using only a banana. Please refrain from posting alternative solutions that resort to lasers." 你的约束就像是说“我想用香蕉制作CD播放器。请不要发布使用激光器的替代解决方案。” GTK can't do that, you're using the wrong tool for the job. GTK无法做到这一点,你正在使用错误的工具来完成工作。

Here's an explanation of what a "window" actually means and why your code doesn't work: 这里解释了“窗口”实际意味着什么以及为什么代码不起作用:

First off, you need to understand the difference between a gtk.Window and a gtk.gdk.Window . 首先,您需要了解gtk.Windowgtk.gdk.Window之间的gtk.gdk.Window A GTK window is a top level GTK widget that can contain other widgets. GTK窗口是顶级GTK窗口小部件,可以包含其他窗口小部件。 It is usually linked to a window on your desktop, but doesn't have to be - in GTK 3 there is an OffscreenWindow . 它通常链接到桌面上的一个窗口,但不一定是 - 在GTK 3中有一个OffscreenWindow

A GDK window, on the other hand, is platform-dependent. 另一方面, GDK窗口与平台有关。 On an X desktop it is a thin wrapper around an X window, which is not necessarily a toplevel desktop window. 在X桌面上,它是X窗口周围的薄包装,不一定是顶级桌面窗口。 On other systems it exists to abstract away the windowing system. 在其他系统上,它可以抽象出窗口系统。 A GDK window receives events, so some GTK non-window widgets have their own GDK windows. GDK窗口接收事件,因此一些GTK非窗口小部件具有自己的GDK窗口。 "Window" is really a terrible name for these objects, but it was inherited from X and it's probably not going to change. “Window”对于这些对象来说确实是一个糟糕的名字,但它是从X继承的,它可能不会改变。

Each GTK process only knows about its own windows. 每个GTK进程只知道自己的窗口。 You can get a list of the toplevel GTK windows of your own application using gtk.window_list_toplevels() . 您可以使用gtk.window_list_toplevels() 自己应用程序的顶级GTK窗口列表。 Getting the children of these windows should return you the GTK widgets that they contain. 获取这些窗口的子项应该返回它们包含的GTK小部件。 However, you can't descend into the widget hierarchy of other processes' windows. 但是,您无法进入其他进程窗口的窗口小部件层次结构。 For example, what if another process has a window with a child widget that is a custom widget that your process doesn't know about? 例如,如果另一个进程有一个窗口,其子窗口小部件是您的进程不知道的自定义窗口小部件,该怎么办? What should it report as the type of that widget? 它应该作为该窗口小部件的类型报告什么?

Getting a list of the toplevel GDK windows with gtk.gdk.window_get_toplevels() is basically the same as getting a list of the toplevel X windows, as far as I understand it. 根据我的理解,使用gtk.gdk.window_get_toplevels()获取顶级GDK窗口列表与获取顶级X窗口列表基本相同。 You have no way of knowing what kind of windows they are - they might be the Gnome Panel, or they might be Qt windows, or they might be something else altogether that doesn't correspond with a desktop window. 您无法知道它们是什么类型的窗口 - 它们可能是Gnome面板,或者它们可能是Qt窗口,或者它们可能是完全与桌面窗口不对应的其他窗口。

Libwnck ( link to the overview of what it does) can get you a list of non-minimized windows, and their titles, but it won't allow you to see inside them. Libwnck( 链接到它的功能概述)可以为您提供非最小化窗口及其标题的列表,但它不允许您查看它们内部。 There's no way to do that. 没有办法做到这一点。 Libwnck uses GDK internally, so technically you could do it using GDK, but why would you bother if there's already a library that does that for you? Libwnck在内部使用GDK,所以从技术上来说你可以使用GDK来做,但是如果已经有一个库可以帮你做到这一点,你为何会烦恼? If you really want to do it yourself, look at the libwnck source code. 如果您真的想自己做,请查看libwnck源代码。

The windows you get are the windows that were created within your process. 您获得的窗口是在您的过程中创建的窗口。 To get the list of windows, you need to query the properties of the root window, like this: 要获取窗口列表,您需要查询根窗口的属性,如下所示:

import gtk.gdk
root = gtk.gdk.get_default_root_window()
for id in root.property_get('_NET_CLIENT_LIST')[2]:
    w = gtk.gdk.window_foreign_new(id)
    if w:
        print(w.property_get('WM_NAME')[2])

Please note that GDK is a thin layer over underlying OS graphics engine (X11/Quartz/Aqua/GDI etc) and result may differ on different NIX devices. 请注意,GDK是底层OS图形引擎(X11 / Quartz / Aqua / GDI等)上的薄层,结果可能因NIX设备而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM