Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 377
john Avatar asked Sep 04 '09 18:09

john


2 Answers

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

like image 148
MestreLion Avatar answered Oct 13 '22 19:10

MestreLion


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()
like image 28
Sandy Avatar answered Oct 13 '22 17:10

Sandy