2012-06-07 2 views
3

VPythonGTK threading에 대한 설명서를 읽었으므로 gtk GUI 내에 VPython 그래픽을 포함시킬 수 있습니다. 나는 그것이 wx on Windows으로 가능하다는 것을 알고 있지만, 나는 리눅스에 있고 PyGTK를 사용하고있다. 이제, 나는 그럭저럭 그 길의 일부를 얻을 수 있었다. VPython 창 을 별도의 프로세스을 생성하는 경우 임베드 할 수 있습니다. 내가 원하는 것은 스레드로 그것을 포함시키는 것입니다. 후자는 소켓 및 네트워크 호출 대신 스레드를 통해 OpenGL을 쉽게 제어 할 수있는 GUI 이벤트를 구현할 수있게합니다.GTK 창 캡처 : VPython (OpenGL) 응용 프로그램

편집 : 분명히 아무도 이것에 대해 아무 것도 모릅니다 ... 맙소사.

다음 코드는 있습니다. 주석 처리 된 두 줄의 주석 처리를 제거하고 몇 가지 명백한 주석을 달아서 프로세스 생성 코드를 얻을 수 있습니다.

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

답변

3

이것은 모든 사람이 염려 할 경우 작동하는 코드입니다.

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

:이 그놈에서 작동하지만, 윈도 매니저에서 작동하지 않습니다. 도형 ...

+0

낡은 실 · 같은 호. 동일한 논리를 구현하는 C 비슷한 문제가 있습니다. GTK 개발 팀에 문의하려고 했습니까? 어쨌든 다른 솔루션을 찾았습니까? 감사합니다 – tmow

+0

@ tmow : 아니, 나는하지 않았다. – Sardathrion

관련 문제