简体   繁体   中英

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

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.

Using GTK on X windows (ie 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.

N.netheless, 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()

Not enough reputation to comment on Glyphs answer. To make xterm work, in addition to the comments above one needs to also add

XTerm*allowSendEvents: True 

to ~/.Xresources . (and perhaps reload those, with xrdb -load ~/.Xresources )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM