13
votes

I started to build a GUI with Glade, python and Gtk3 libraries. I want to try to view an external window linking it inside a container in my GUI. Is it possible? Which is the best container object to do this?

I started to search but easy methods in Gtk2 (like here) can't be used any more in Gtk3.

I found a very interesting post here which allow me to set the right ID of the target window but I'm still confused on how to show it inside my GUI.

I unsucessfully tried to change a bit these tutorials with cairo.

My piece of code so far ( I want to display the window with ID = 0x360000b in the map_area container and I have to use the "self." handles structure). Temporarily the map_area container is a Drawingarea.

# if condition occurs    
    Gdk.Window.process_all_updates()
    win_id = 0x360000b # from xwininfo command
    root_win = GdkX11.X11Display.get_default()
    win = GdkX11.X11Window.foreign_new_for_display(root_win, win_id)
    width = win.get_width()
    height = win.get_height()   
    self.map_area = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)               
    pixbuf = Gdk.pixbuf_get_from_window(win, 0, 0, width, height)
    cr = cairo.Context(self.map_area)   
    Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0)
    cr.paint()

Can anyone help me? Thank you in advance!

1
Have a look at developer.gnome.org/gtk3/stable/GtkSocket.html - this solution requires some cooperation from the other process though.Phillip
Could it work even for embedding external process? I'm interested in a window not generated as a widget in my script. It could be like I want to embed Ubuntu terminal window or the current Mozilla Firefox browser window in a proper widget (I don't know which it could be) inside my gtk main windowmarcoresk
Sure, that's what the documentation means by "other" process. But the external process needs to cooperate with yours; namely, it must actively reparent its top-level window to the window created by the GtkSocket. E.g., xterm has the -into option for this. (If you are willing to add Xlib as a dependency to your script, you can do this for the other application. I'll post an answer with an example.)Phillip
(Turns out, reparenting is not that easy. There is XReparentWindow, and you also have access to that function from GdkX11 through the reparent function of a window, but at least xterm exits if one tries to reparent an already visible window. So there will be no example from me, sorry.)Phillip
I attempted this some time ago and ended up going in a bit of a different direction as I ran into issues like you. All I ended up needing was a screenshot of the application, so I wrote something to grab a screenshot of the window specifically based off the title of the window for Mac and Linux. It also uses X11 and Quartz to do all of the heavy lifting, so it is actually pretty fast. The repository is located here: github.com/Kush131/PyFastCap. It is quite ugly and hacky, but maybe it can help you!Kush

1 Answers

1
votes

It sounds like you want to be a window manager. Have a look at the answers to questions about how to write a window manager, such as this one:

Creating a window manager for Linux

or this one:

Building a Window Manager

Good luck!