Pygtk: reparented windows responding to size allocations

Today’s post is an obscure technical one that I imagine will be completely irrelevant to all of my normal readers at the time I write this. I write to save someone in the future from the hours of debugging and Googling that I had to do to solve this problem.

The Situation

I am writing a Python GTK program. The program reparents another application’s window (let’s call it window A) using gdk.window_foreign_new() and gdk.Window.reparent(). I have written a custom GTK widget that wraps the GDK representation of window A, complete with a do_size_allocate() method so that when the widget is resized, the size allocation is passed on to window A.

The Problem

Everything works fine for very simple applications using my custom widget, but I run into problems when I try to add the custom widget as a descendant of an EventBox. Under the hood the event box has its own window (let’s call it window E). The symptoms that I see make it look as if window A has mistakenly been placed in my application’s root window rather than being placed in window E. That is to say, if window E should appear at offset (5, 12) compared to window E, it instead appears at offset (5, 12) compared to the application’s root window.

The Solution

After much investigation and tinkering, and a certain amount of frustration, I stumbled across something that made everything just work, and before long, I narrowed it down to this line of code, run just before (after might work too) the code that reparents window A:

self.get_parent_window().xid

I tried self.get_parent().realize() at first, but that had no effect. For some reason, accessing the xid (underlying system window id) of the parent window causes everything to magically work. Go figure.

This entry was posted in midlength and tagged , , , , . Bookmark the permalink.

Comments are closed.