如何使用pygtk获得gnome2桌面上所有窗口的列表? [英] How do I get a list of all windows on my gnome2 desktop using pygtk?

查看:166
本文介绍了如何使用pygtk获得gnome2桌面上所有窗口的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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()
>>> get_screen(0).get_root_window()。get_children()
[< gtk.gdk.Window object at at 0x89dcc84(GdkWindow at 0x8a4c170)>,> gtk.gdk.Window object at 0x89dccac(GdkWindow at 0x8a4c0c0)>]

如控制台输出中所示,第二个选项返回两个视窗。但我一直无法弄清楚它们是什么。



任何人都可以解释基于典型gtk的对象的层次结构吗?没有人有任何孩子,我总是得到这两个窗口,无论我在桌面上有多少个窗口。桌面环境?
我无法理解为什么上面的代码不起作用。



请不要发布替代解决方案,将资源转换为wnck,xlib,qt等。我更感兴趣的是理解我在做什么错误,而不是像在获取建议时那样我们检查其他库。 你的约束是比如说:我想用一根香蕉建立一个CD播放机,请不要发布诉诸激光的替代解决方案。 GTK无法做到这一点,你使用的工具是错误的。



下面是一个窗口实际意义的解释,以及为什么你的代码不' t工作:首先,您需要了解 gtk.Window 和a <$ c之间的区别$ C> gtk.gdk.Window 。 GTK窗口是一个顶级GTK小部件,可以包含其他小部件。它通常链接到桌面上的窗口,但不一定是 - 在GTK 3中有一个 OffscreenWindow



另一方面,GDK 窗口与平台相关。在X桌面上,它是一个X窗口的薄包装,它不一定是顶级桌面窗口。在其他系统上,它可以抽象出窗口系统。 GDK窗口接收事件,因此一些GTK非窗口小部件具有其自己的GDK窗口。 Window对于这些对象来说确实是一个可怕的名字,但它是从X继承而来的,它可能不会改变。



每个GTK进程只知道它的自己的窗口。您可以使用自己的应用程序的顶级GTK窗口列表--window-list-toplevelsrel =noreferrer> gtk.window_list_toplevels() 。获取这些窗口的子项应该返回它们包含的GTK小部件。但是,您无法下载到其他进程窗口的窗口小部件层次结构中。例如,如果另一个进程有一个带有子进程的窗口,该进程不知道什么是自定义窗口小部件?它应该报告为该窗口小部件的类型?



使用 gtk.gdk.window_get_toplevels() 基本上与获得顶层X窗口的列表相同,据我了解。你无法知道它们是什么样的窗口 - 它们可能是Gnome面板,或者它们可能是Qt窗口,或者它们可能完全不同于桌面窗口。



Libwnck(连结至概览它可以给你一个非最小化的窗口列表和它们的标题,但它不会让你看到它们。没有办法做到这一点。 Libwnck在内部使用GDK,所以在技术上你可以使用GDK来做,但是如果已经有一个库可以为你做这些工作,你为什么还要打扰呢?如果您真的想自己做,请查看libwnck源代码。


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.

解决方案

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.

这篇关于如何使用pygtk获得gnome2桌面上所有窗口的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆