FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 23.44 | Next Entry >>

23.44. How can I embed a Jython application in PyGTK?

I'm glad you asked, it so happens I know how to do this.

On the Java side, you need to create an XEmbeddedFrame (or a Windows on if you're on windows:

 import sys
 from javax.swing import JButton
 from sun.awt.X11 import XEmbeddedFrame

 def printMessage(event):
    print 'Hello from Java!'

 window_id = long(sys.argv[1])
 frame = XEmbeddedFrame(window_id)
 frame.setVisible(True)
 frame.setSize(300, 300)
 button = JButton("Push Me!", actionPerformed=printMessage)
 frame.add(button)
Notice that you need to send a window-id of the window where you want to embed the java swing widget.

On the python side we do the following:

 import os
 import gtk

 def clicked(button, socket):
    print os.system('jython jframe.py %d &' % (socket.get_id(),))

 win = gtk.Window()
 win.connect('delete-event', gtk.main_quit)
 vbox = gtk.VBox()
 win.add(vbox)
 socket = gtk.Socket()
 vbox.pack_start(socket)
 button = gtk.Button('Launch')
 button.connect('clicked', clicked, socket)
 vbox.pack_start(button, False, False)
 win.set_size_request(640, 480)
 win.show_all()
 gtk.main()
Nothing fancy here apart from the fact that we use a gtk.Socket() to embed the java application and launching it by sending in socket.get_id() via os.system.

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org