views:

640

answers:

4

How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?

edit:

i want get list paths opened directories on desktop!

+1  A: 

From the PyGTK reference:

gtk.gdk.window_get_toplevels()

The gtk.gdk.window_get_toplevels() function returns a list of all toplevel windows known to PyGTK on the default screen. A toplevel window is a child of the root window (see the gtk.gdk.get_default_root_window() function).

Donald Harvey
It returns only one window - "('UTF8_STRING', 8, 'wnd.py')", and i have three opened gedit windows
john
I'm getting the same issue after testing - the function didn't work as the reference suggested it would. Your best bet for now is probably instead to use the call to wmctrl -l as suggested by NawaMan; aside from that the only thing I can think of is directly using the X11 module.
Donald Harvey
+1  A: 

I really don't know how to check if a window is a GTK one. But if you want to check how many windows are currently open try "wmctrl -l". Install it first of course.

NawaMan
+2  A: 

You probably want to use libwnck:

http://library.gnome.org/devel/libwnck/stable/

I believe there are python bindings in python-gnome or some similar package.

Once you have the GTK+ mainloop running, you can do the following:

import wnck
window_list = wnck.screen_get_default().get_windows()

Some interesting methods on a window from that list are get_name() and activate().

This will print the names of windows to the console when you click the button. But for some reason I had to click the button twice. This is my first time using libwnck so I'm probably missing something. :-)

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

class WindowLister:
    def on_btn_click(self, widget, data=None):
        window_list = wnck.screen_get_default().get_windows()
        if len(window_list) == 0:
         print "No Windows Found"
        for win in window_list:
            print win.get_name()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.button = gtk.Button("List Windows")
        self.button.connect("clicked", self.on_btn_click, None)

        self.window.add(self.button)
        self.window.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    lister = WindowLister()
    lister.main()
Sandy
wnck.screen_get_default().get_windows() - return empty list :(
john
In my quick test, I had to bring up a gtk+ window (in my python process) before the call would work.
Sandy
You can give a simple example code?thx
john
If your app is non-GUI, you can do while "gtk.events_pending(): gtk.main_iteration()" after "wnck.get_screen_default()" to flush events and be able to get the list of windows.
akaihola
+1  A: 

Parsing command line output is usually not the best way, you are dependent on that programs output not changing, which could vary from version or platfrom. Here is how to do it using Xlib:

import Xlib.display

screen = Xlib.display.Display().screen()
root_win = screen.root

window_names = []
for window in root_win.query_tree()._data['children']:
    window_name = window.get_wm_name()
    window_names.append(window_name)

print window_names

Note that this list will contain windows that you wouldn't normally classify as 'windows', but that doesn't matter for what you are trying to do.

DoR