GTK窗口捕获:VPython(OpenGL)应用程序 [英] GTK window capture: VPython (OpenGL) application

查看:110
本文介绍了GTK窗口捕获:VPython(OpenGL)应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已阅读 VPython 在Windows上使用wx ,但是我在Linux上并且正在使用PyGTK.现在,我设法参与其中了.我可以嵌入一个VPython窗口,前提是它是由一个单独的进程生成的..我想要将其作为线程嵌入.后者将使控制OpenGL的GUI事件更易于实现-通过线程而不是套接字和网络调用.

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.

显然没有人对此有任何了解...嗯.

这是我的代码.取消注释两行注释掉的注释,并注释一些明显的其他注释,您可以获取生成代码的过程.

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()

推荐答案

这是在任何人关心的情况下都可以使用的代码.

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()

注意:这在Gnome下不起作用,但在fvwm2下起作用.走吧...

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

这篇关于GTK窗口捕获:VPython(OpenGL)应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆