Re: gtk embedding.

Yann Golanski <ygolanski-hnK7fAIlAj/[email protected]> Tue, 12 Jun 2012 08:11:20 +0100
Newsgroups gmane.comp.python.visualpython.user
Message-ID <[email protected]>
Hi,

Here is an example of embedding VPython within a GTK GUI.  It is a trivial
skeleton type of thing.  Process communication can be done via a named file
(FIFO) pipe or a Queue().  

Feel free to add this example to your project if you think it will help
others.

================================================================================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import sys
import os
import re
import time
from visual import *


def find_window_id (title):
    """Gets the OpenGL window ID."""
    pattern = re.compile('0x[0-9abcdef]{7}')
    proc = subprocess.Popen(['xwininfo', '-name', title],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    errors = proc.stderr.readlines()
    if errors:
        return None
    for line in proc.stdout.readlines():
        match = pattern.findall(line)
        if len(match):
            return long(match[0], 16)
    return None



class Setting ():
    """VPython/OpenGL class."""

    def __init__ (self, w=256, h=256, title='OpenGL via VPython'):
        """Initiator."""
        self.width = w
        self.height = h
        self.title = title
        self.scene = display.get_selected() 
        self.scene.title = self.title
        self.scene.width = self.width
        self.scene.height = self.height
        self.sphere = sphere()



class GTKDisplay ():

    def __init__ (self, winID):
        """Initiator: Draws the GTK GUI."""
        import gtk
        import pygtk
        self.OpenGLWindowID = winID
        window = gtk.Window()
        window.show()
        socket = gtk.Socket()
        socket.show()
        window.add(socket)
        window.connect("destroy", lambda w: gtk.main_quit())
        socket.add_id(long(self.OpenGLWindowID))
        gtk.main()



def main ():
    """Main entry point."""
    name = 'sphere OpenGL window'
    child_pid = os.fork()
    if 0 == child_pid:
        sut = Setting(title=name)
    else:
        winID = None
        while not winID:
            time.sleep(.1)
            winID = find_window_id(name)
        try:
            gui = GTKDisplay(winID)
        except KeyboardInterrupt, err:
            print '\nAdieu monde cruel!'


if __name__ == "__main__":
    main()
================================================================================

-- 
GPG Fingerprint 8666 7F9A 17E7 AC8E CC36  1D1E C707 D658 6685 2528
   The views expressed in this e-mail may or may not reflect those of the
   author and do not necessarily reflect those of Isotek Oil and Gas Ltd.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Visualpython-users mailing list
Visualpython-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/visualpython-users
signature.asc (application/pgp-signature, 490 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQEcBAEBAgAGBQJP1uuYAAoJEMcH1lhmhSUougYH/RJ+CiNqDxW0mFFX6dYzr5j1
395QDrisX3/NJ2llEeSItkk5zfFLAnNFfaXVh/rim/2f4RrrrQf6lqKh1eMd2mdK
tmPpsOA498groD5gFJBroKiLnPJfl8gsw8TEhNoVCQbQV5GzmRIGuyXCvN03Ldrv
PqnE9d18bNi8gY97gCWfODamnXtXWubpMUAx56DWmznEqrYWmu9ZabHM1wZnWnO3
O8TPAvk9PSAeWTZsikKLswehFSTQ3DY5PZzLVmftp5HP1wTw7sEdEGnxE8pYXuKu
KF0Rb4yk8wTNgK60b7YpbRy/XCFbj/ck443rmeGxVACsaRptoHFGgSLRDpoNrRk=
=1hwK
-----END PGP SIGNATURE-----