Skip to content

Instantly share code, notes, and snippets.

@adewes
Last active February 28, 2024 23:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adewes/6960581 to your computer and use it in GitHub Desktop.
Save adewes/6960581 to your computer and use it in GitHub Desktop.
Get the active window in Gtk using the wnck library.
import wnck
import gtk
import time
if __name__ == '__main__':
screen = wnck.screen_get_default()
screen.force_update()
while True:
while gtk.events_pending():
gtk.main_iteration()
time.sleep(0.5)
print screen.get_active_window().get_name()
@ganesshkumar
Copy link

while gtk.events_pending():
    gtk.main_iteration()

Why is this part needed?

@adewes
Copy link
Author

adewes commented Oct 24, 2013

It seems that gtk does not update the window title unless you process all pending events first. If you remove the two lines, you should see that the window title will not be updated correctly (at least on my system that's the case). I'm not entirely sure why GTK behaves this way myself, but this hack seems to fix it.

@kasulamadhusudhan
Copy link

Thanks Adewes !!!

@SarveshMD
Copy link

I wish I found this 7 hours ago. I was very confused why my program doesn't upgrade it's window and I was totally mad for the past 7 hours trying to figure out what's the problem. I actually thought that the screen.force_update() was doing the update, but now I know it doesn't. The

while gtk.events_pending():
    gtk.main_iteration()

lines were my gem. Thanks a ton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment