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

发布于 2025-01-07 06:57:03 字数 856 浏览 2 评论 0原文

我对 gtk 和 gnome 的一些概念有点困惑。我试图在 gnome2 桌面上获取非最小化窗口的列表,但在阅读 pygtk 文档并检查结果后,我无法理解结果。

下面的两个片段似乎都不起作用。

首先我尝试了这个..

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

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

然后这个

>>> 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)>] 

如控制台输出中所示,第二个选项返回两个窗口。但我一直无法弄清楚它们是什么。他们都没有孩子,无论我的桌面上有多少窗口,我总是会得到这两个窗口。

有人能解释一下典型的基于 gtk 的桌面环境的对象层次结构吗? 我不明白为什么上面的代码不起作用。

请不要发布 wnck、xlib、qt 等资源的替代解决方案。我更感兴趣的是了解我做错了什么,而不是获取建议(例如检查其他库)。

I'm a bit confused with some gtk and gnome concepts. 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.

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?
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2025-01-14 06:57:03

你的限制就像是说“我想只用一根香蕉制作一个 CD 播放器。请不要发布求助于激光的替代解决方案。” GTK 无法做到这一点,您使用了错误的工具来完成这项工作。

以下是“窗口”实际含义以及代码不起作用的原因的解释:

首先,您需要了解 gtk.Windowgtk.gdk 之间的区别。窗口。 GTK 窗口是顶级 GTK 小部件,可以包含其他小部件。它通常链接到桌面上的窗口,但并非必须如此 - 在 GTK 3 中有一个 OffscreenWindow

另一方面,GDK 窗口是依赖于平台的。在 X 桌面上,它是 X 窗口的薄包装,而 X 窗口不一定是顶级桌面窗口。在其他系统上,它的存在是为了抽象出窗口系统。 GDK 窗口接收事件,因此一些 GTK 非窗口小部件有自己的 GDK 窗口。对于这些对象来说,“Window”确实是一个糟糕的名字,但它是从 X 继承的,并且可能不会改变。

每个 GTK 进程只知道它自己的窗口。您可以使用 gtk.window_list_toplevels()。获取这些窗口的子窗口应该会返回它们包含的 GTK 小部件。但是,您无法进入其他进程窗口的小部件层次结构。例如,如果另一个进程有一个带有子窗口小部件的窗口,而该子窗口小部件是您的进程不知道的自定义窗口小部件,该怎么办?它应该报告什么作为该小部件的类型?

使用 gtk.gdk.window_get_toplevels() 据我了解,与获取顶级 X 窗口列表基本相同。您无法知道它们是什么类型的窗口 - 它们可能是 Gnome 面板,或者它们可能是 Qt 窗口,或者它们可能是与桌面窗口不对应的其他东西。

Libwnck(链接到其功能概述)可以为您提供非最小化窗口的列表及其标题,但它不允许您查看它们的内部。没有办法做到这一点。 Libwnck 在内部使用 GDK,因此从技术上讲,您可以使用 GDK 来完成此操作,但如果已经有一个库可以为您完成此操作,为什么还要麻烦呢?如果你真的想自己动手,请查看 libwnck 源代码。

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." GTK can't do that, you're using the wrong tool for the job.

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. A GTK window is a top level GTK widget that can contain other widgets. It is usually linked to a window on your desktop, but doesn't have to be - in GTK 3 there is an OffscreenWindow.

A GDK window, on the other hand, is platform-dependent. On an X desktop it is a thin wrapper around an X window, which is not necessarily a toplevel desktop window. 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. "Window" is really a terrible name for these objects, but it was inherited from X and it's probably not going to change.

Each GTK process only knows about its own windows. You can get a list of the toplevel GTK windows of your own application using gtk.window_list_toplevels(). Getting the children of these windows should return you the GTK widgets that they contain. 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. 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.

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. 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? If you really want to do it yourself, look at the libwnck source code.

┾廆蒐ゝ 2025-01-14 06:57:03

您获得的窗口是在您的进程中创建的窗口。要获取窗口列表,您需要查询根窗口的属性,如下所示:

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])

请注意,GDK 是底层操作系统图形引擎(X11/Quartz/Aqua/GDI 等)上的薄层,结果可能会因不同的操作系统而异。 NIX 设备。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文