I want to bring a given window to the top and give it focus.  I made the program below to test this.  The program displays the name of each window in the window tree in a text control.  Clicking on a line in the text control should cause the corresponding window to come to the front and get the focus.  It doesn't work -- while the window gets the focus, it doesn't get raised until I close the window I created in the program.  How can I get this working?

I using Ubuntu 8.04 / Gnome / Metacity.

------------------

import wx, wx.stc

import Xlib.display
from Xlib import X

app = wx.App(redirect=False)
frame = wx.Frame(None)
panel = wx.Panel(frame)
panel.SetSizer(wx.BoxSizer(wx.VERTICAL))

display = Xlib.display.Display()
root_window = display.screen().root

class MyTextControl(wx.stc.StyledTextCtrl):
    def __init__(self):
        wx.stc.StyledTextCtrl.__init__(self, panel)
        self.Bind(wx.EVT_LEFT_UP, self.on_left_up)   
    
    def on_left_up(self, e):
        e.Skip()
        window = windows[self.GetCurrentLine()]
        window.configure(stack_mode=X.Above)
        window.set_input_focus(X.RevertToParent, X.CurrentTime)
        display.sync()
        
text_control = MyTextControl()

panel.GetSizer().Add(text_control, flag=wx.EXPAND, proportion=1)


def each_window(window, depth):
    yield window, depth
    for child in window.query_tree().children:
        for grandchild, new_depth in each_window(child, depth + 1):
            yield grandchild, new_depth

windows = []
for window, depth in each_window(root_window, 0):
    windows.append(window)    
    text_control.AppendText(depth * 2 * ' ' + str(window.get_wm_name()) + "\n")

frame.Show()
app.MainLoop()