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!
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!
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 thegtk.gdk.get_default_root_window()
function).
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.
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()
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.