Question

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)
Was it helpful?

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()))

OTHER TIPS

To get away from the Wnck-WARNING, you need to send a valid timestamp with the w.activate() function. The way that I found to do this is to use:

now = gtk.gdk.x11_get_server_time(gtk.gdk.get_default_root_window())

w.activate(now)

There really should be an easier way to do this, or wnck should allow a timestamp of 0 to mean now like most of the gtk libraries use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top