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
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.
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.
For whatever reason, I can't post a comment, but I'd like to add this as an addendum to Sandy's answer.
Here's a chunk of code that lists the current windows on the console:
import pygtk
pygtk.require('2.0')
import gtk, wnck
if __name__ == "__main__":
default = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration(False)
window_list = default.get_windows()
if len(window_list) == 0:
print "No Windows Found"
for win in window_list:
if win.is_active():
print '***' + win.get_name()
else:
print win.get_name()
Thanks Sandy!
Welcome to 2013! Here's the code using Wnck
and its modern GObject Introspection libraries instead of the now deprecated PyGTK method. You may also check my other answer about wnck:
from gi.repository import Gtk, Wnck
Gtk.init([]) # necessary only if not using a Gtk.main() loop
screen = Wnck.Screen.get_default()
screen.force_update() # recommended per Wnck documentation
# loop all windows
for window in screen.get_windows():
print window.get_name()
# ... do whatever you want with this window
# clean up Wnck (saves resources, check documentation)
window = None
screen = None
Wnck.shutdown()
As for documentation, check out the Libwnck Reference Manual. It is not specific for python, but the whole point of using GObject Introspection is to have the same API across all languages, thanks to the gir
bindings.
Also, Ubuntu ships with both wnck
and its corresponding gir
binding out of the box, but if you need to install them:
sudo apt-get install libwnck-3-* gir1.2-wnck-3.0
This will also install libwnck-3-dev
, which is not necessary but will install useful documentation you can read using DevHelp
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()