How to embed some application window in my application using any Python GUI framework

python, user-interface

Solution

Using GTK on X windows (i.e. Linux, FreeBSD, Solaris), you can use the XEMBED protocol to embed widgets using `gtk.Socket`. Unfortunately, the application that you're launching has to explicitly support it so that you can tell it to embed itself. Some applications don't support this. Notably, I can't find a way to do it with Firefox.

Nonetheless, here's a sample program that will run either an X terminal or an Emacs session inside a GTK window:

import os
import gtk
from gtk import Socket, Button, Window, VBox, HBox

w = Window()
e = Button("Emacs")
x = Button("XTerm")
s = Socket()
v = VBox()
h = HBox()
w.add(v)
v.add(s)
h.add(e)
h.add(x)
v.pack_start(h, expand=False)

def runemacs(btn):
    x.set_sensitive(False); e.set_sensitive(False)
    os.spawnlp(os.P_NOWAIT, "emacs", 
        "emacs", "--parent-id", str(s.get_id()))

def runxterm(btn):
    x.set_sensitive(False); e.set_sensitive(False)
    os.spawnlp(os.P_NOWAIT, "xterm",
        "xterm", "-into", str(s.get_id()))

e.connect('clicked', runemacs)
x.connect('clicked', runxterm)
w.show_all()
gtk.main()

Problem

I want some application to look like widget inside my Python application. That's all. I dont need any interaction between them. I'm interested in solutions in any GUI toolkit for both windows and x windows. It would be nice to have a solution with Tkinter but it's not crucial.

Original source