5

I get this code on CEF Python 3 (link)

    ...

    self.container = gtk.DrawingArea()
    self.container.set_property('can-focus', True)
    self.container.connect('size-allocate', self.OnSize)
    self.container.show()

    ...

    windowID = self.container.get_window().handle
    windowInfo = cefpython.WindowInfo()
    windowInfo.SetAsChild(windowID)
    self.browser = cefpython.CreateBrowserSync(windowInfo,
            browserSettings={},
            navigateUrl=GetApplicationPath('example.html'))

    ...

This code [self.container.get_window().handle] don't work with PyGI and GTK3.

I trying port the code from GTK2 to GTK3, how I can do this?

Edited:


After some search, I found a tip to make get_window work: I call: self.container.realize() before self.container.get_window(). But I cant't get Window Handle yet.

I need put CEF3 window inside a DrawingArea or any element. How I can do this with PyGI?

Edited:


My environment is:

Windows 7

Python 2.7 and Python 3.2

ePhillipe
  • 73
  • 1
  • 7
  • `self.container.get_window()` should work, but it might return `None` if the `GtkWidget` is not realized yet. Maybe you can pass the output of `get_window()` to `SetAsChild()`? You should not need to know the low-level window handles. – Torkel Bjørnson-Langen Apr 14 '14 at 13:53
  • https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html – Torkel Bjørnson-Langen Apr 14 '14 at 13:54
  • @TorkelBjørnson-Langen, thanks for response. I try use the get_window() result with SetAsChild(). However, SetAsChild() method expect a integer parameter. I need XID of this get_window() to pass to SetAsChild(). Do you known how I can get it? – ePhillipe Apr 15 '14 at 12:30
  • No I do not. But this post might help you: http://stackoverflow.com/questions/14732838/how-to-get-xwindow-id-in-gtk3 – Torkel Bjørnson-Langen Apr 16 '14 at 16:34
  • @TorkelBjørnson-Langen, thanks for your help. I cannot found a Python Binding that work in Win32. I try everything. Do you known if exist an alternative? – ePhillipe Apr 23 '14 at 17:51

3 Answers3

5

Sadly there seems to be no progress on the python gobject introspection to fix this and make gdk_win32_window_get_handle available (reported a bug in the gnome bugtracker quite a while ago) - it is also quite needed for Python GStreamer and Windows ...

So I followed the suggestion of totaam and used ctypes to access gdk_win32_window_get_handle. Took me forever since I had no experience with this - and well it is somehow quite an ugly hack - but well when needed...

Here is the code:

        Gdk.threads_enter()            
        #get the gdk window and the corresponding c gpointer
        drawingareawnd = drawingarea.get_property("window")
        #make sure to call ensure_native before e.g. on realize
        if not drawingareawnd.has_native():
            print("Your window is gonna freeze as soon as you move or resize it...")
        ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
        ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object]
        drawingarea_gpointer = ctypes.pythonapi.PyCapsule_GetPointer(drawingareawnd.__gpointer__, None)            
        #get the win32 handle
        gdkdll = ctypes.CDLL ("libgdk-3-0.dll")
        hnd = gdkdll.gdk_win32_window_get_handle(drawingarea_gpointer)
        #do what you want with it ... I pass it to a gstreamer videosink
        Gdk.threads_leave()
  • On my PC, calling `gdk_win32_window_get_handle` like this throws an error: `ctypes.ArgumentError: argument 1: : int too long to convert`. Doing `gdkdll.gdk_win32_window_get_handle.argtypes = [ctypes.c_void_p]` beforehand appears to fix this. – Aran-Fey Dec 06 '21 at 12:30
0

You must first import GdkX11 for get_xid() to be available on the returned GdkX11Window.

from gi.repository import GdkX11

...

-windowID = self.container.get_window().handle
+windowID = self.container.get_window().get_xid()
  • I forget mention my environment. I develop for Windows and work on Windows 7 64bits. I already try import GdkX11 but its not work on Windows. – ePhillipe Apr 16 '14 at 20:18
  • Well, Windows® uses HWND as it's native window handle (not XID), so you have to adapt to that. I do not have Windows so I can not help. In plain C I would use the `GDK_WINDOW_HWND` macro from `gdk/gdkwin32.h`. – Torkel Bjørnson-Langen Apr 17 '14 at 13:05
  • In GTK+2 works in Windows®. The code _self.container.get_window().handle_ works very well. – ePhillipe Apr 17 '14 at 16:38
0

The answer advising you to use .handle or .get_xid() works on GTK2, but not with GTK3 or on MS Windows, which are part of your question.

I've done a lot of digging and found that there is a function in GTK3 which does what you want: gdk_win32_window_get_handle, but sadly it is not available in the gi bindings. You may be able to get to it using ctypes or Cython (which is what I am going to do).

totaam
  • 1,306
  • 14
  • 25