How to get Gdk window from xid?

2019-02-13 09:49发布

问题:

I'm lost in version 3.. in python2+gdk2 is:

#!/usr/bin/env python2
import gtk

gtk.gdk.window_process_all_updates()
window_xid = 54525964
gdk_window = gtk.gdk.window_foreign_new(window_xid)

which is pretty much straight forward. But then, the horror:

#!/usr/bin/env python3
from gi.repository import xlib
from gi.repository import Gdk
from gi.repository import GdkX11

Gdk.Window.process_all_updates()
xlib_window = "???????"
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)

the xlib is killing me.. I'm unable to do anything with it. Has anybody worked with it before??

The documentation I've through several times already is: Gdk3, Xlib

Getting the window from its xid was the fastest way to get a screenshot in python2 I guess I'll have try another way in python3.. any ideas? maybe peek_children from root window?? but then, how do I know if it's the window I want?

回答1:

A Window in X11 is the same as an XID. There's just a typedef from one to the other.

So in C code gdk_x11_window_foreign_new_for_display() just accepts an Window or XID, which is basically an integer. This also works in python using introspection:

#!/usr/bin/env python3
from gi.repository import Gdk
from gi.repository import GdkX11

Gdk.Window.process_all_updates()
xlib_window = 0x2a00005 # from xwininfo command
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
print(gdk_window.get_geometry())