Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:42:23+00:00 2026-06-05T16:42:23+00:00

Having read the documentation for VPython and GTK threading , it seems to me

  • 0

Having read the documentation for VPython and GTK threading, it seems to me that it would be possible to embed VPython graphics within a gtk GUI. I know that it is possible with wx on Windows but I am on Linux and using PyGTK. Now, I have managed to get part of the way. I can embed a VPython window provided that it is spawned a separate process. What I would like is to embed it as a thread. The latter would make GUI events that control the OpenGL easier to implement — via a thread instead of a socket and network calls.

Edit: Apparently nobody knows anything about this… Meh.

Here is the code I have. Uncomment the two commented out lines and comment a few obvious others and you can get to the process spawning code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from visual import *
import threading
import Queue
import gtk
import pygtk
import re
import subprocess


class OPenGLThreadClass (threading.Thread):
    """Thread running the VPython code."""

    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.name = 'OpenGLThread'


    def run (self):
        gtk.threads_enter()
        self.scene = display.get_selected() 
        self.scene.title = 'OpenGL test'
        s = sphere()
        gtk.threads_leave()
        #P = subprocess.Popen(['python', 'opengl.py'])
        time.sleep(2)
        self.queue.put(self.find_window_id())
        self.queue.task_done()


    def find_window_id (self):
        """Gets the OpenGL window ID."""
        pattern = re.compile('0x[0-9abcdef]{7}')
        P = subprocess.Popen(['xwininfo', '-name', self.scene.title],
        #P = subprocess.Popen(['xwininfo', '-name', 'Visual WeldHead'],
                stdout=subprocess.PIPE)
        for line in P.stdout.readlines():
            match = pattern.findall(line)
            if len(match):
                ret = long(match[0], 16)
                print("OpenGL window id is %d (%s)" % (ret, hex(ret)))
                return ret


class GTKWindowThreadClass (threading.Thread):
    """Thread running the GTK code."""

    def __init__ (self, winID):
        threading.Thread.__init__(self)
        self.OpenGLWindowID = winID
        self.name = 'GTKThread'


    def run (self):
        """Draw the GTK GUI."""
        gtk.threads_enter()
        window = gtk.Window()
        window.show()
        socket = gtk.Socket()
        socket.show()
        window.add(socket)
        window.connect("destroy", lambda w: gtk.main_quit())
        print("Got winID as %d (%s)" % (self.OpenGLWindowID, hex(self.OpenGLWindowID)))
        socket.add_id(long(self.OpenGLWindowID))
        gtk.main()
        gtk.threads_leave()



def main ():
    thread = {}
    print("Embedding OpenGL/VPython into GTK GUI")
    queue = Queue.Queue()
    thread['OpenGL'] = OPenGLThreadClass(queue)
    thread['OpenGL'].start()
    winID = queue.get()
    print("Got winID as %d (%s)" % (winID, hex(winID)))
    gtk.gdk.threads_init()
    thread['GTK'] = GTKWindowThreadClass(winID)
    thread['GTK'].start()



if __name__ == "__main__":
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:42:25+00:00Added an answer on June 5, 2026 at 4:42 pm

    This is the code that works in case anyone cares.

    #!/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()
    

    Note: This does not work under Gnome but works under fvwm2. Go figure…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble finding a class or API in Android documentation that would
Having read the documentation of Java's String class, it doesn't appear to support popping
Having read Apple's numerous documentation on icon guidelines: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/MobileHIG/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW2 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/App-RelatedResources/App-RelatedResources.html#//apple_ref/doc/uid/TP40007072-CH6-SW1 http://developer.apple.com/library/ios/#qa/qa1686/_index.html I was wondering
Having read other people's questions I thought window.onload=... would answer my question. I have
Having read around it appears this should be possible. I have the following content
I recall having read somewhere that it is better (in terms of performance) to
Having read the documentation for the remove-function at http://api.jquery.com/remove/ , was under the impression
I remember having read this in Apple's documentation but now I can't find it.
I'm having trouble with isdigit. I read the documentation, but when I cout <<
Having read the documentation and many many articles I believe the following should work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.