Search code examples
pythonwnck

python-wnck set focus to window


I'm trying to set focus to a window using python-wnck.
The only docs I could find related to this library are from https://developer.gnome.org/libwnck/stable/WnckWindow.html

Using some code I found on another question here at SO, I was able to search for windows using the window title, but I'm not sure how to get a window to focus.
From the above docs I found the function:

wnck_window_activate(WnckWindow *window, guint32 timestamp);


So in python I tried using this function like "window.activate(0)", but this appears to fail, the icon on my taskbar flashed but it doesn't get focus.
In the terminal I get the message:

 (windowTest.py:17485): Wnck-WARNING: Received a timestamp of 0; window activation may not function properly

So I think I may actually need to put in a valid timestamp but not sure how to get this.

This is the code Im using sofar:

import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

titlePattern = re.compile('.*Geany.*')

windows = screen.get_windows()
for w in windows:
  if titlePattern.match(w.get_name()):
    print w.get_name()
    w.activate(0)

Solution

  • The solution was actually pretty simple
    I just needed to "import time" then pass "int(time.time())" into the activate function

    Working code:

    import pygtk
    pygtk.require('2.0')
    import gtk
    import wnck
    import re
    import sys
    import time
    
    screen = wnck.screen_get_default()
    while gtk.events_pending():
        gtk.main_iteration()
    
    titlePattern = re.compile('.*Geany.*')
    
    windows = screen.get_windows()
    for w in windows:
      if titlePattern.match(w.get_name()):
        print w.get_name()
        w.activate(int(time.time()))