Hi all,

There is a thread on matplotlib and ipython mailing list discussing
the use of PyOS_InputHook for interactive GUI instead of using
dedicate thread. Robin Dunn and Brian Granger posted a nice code
showing example for the wx framework. I tried to adapt it for pyglet
and it seems to work (code below). Just start python and import the
code, you should have a running window and be able to change text
label from within console.

Tested on macos. Feedbacks welcome for windows and linux...

Nicolas

"""
Enable pyglet to be used interacive by setting PyOS_InputHook.

Based on inputhook_wx.py by Robin Dunn and Brian Granger
Adapted for pyglet by Nicolas Rougier.

See thread: Ctypes based prototype of PyOS_InputHook for wx 2.8 and
2.9
              (on matplotlib-devel and Ipython-devel mailing lists)
"""
import os
import sys
import time
from ctypes import *
import pyglet

if os.name == 'posix':
    import select
elif sys.platform == 'win32':
    import msvcrt

pyos_inputhook_ptr = c_void_p.in_dll(pythonapi, "PyOS_InputHook")
orig_pyos_inputhook_ptr_value = pyos_inputhook_ptr.value


def stdin_ready():
    """ """
    if os.name == 'posix':
        infds, outfds, erfds = select.select([sys.stdin],[],[],0)
        if infds:
            return True
        else:
            return False
    elif sys.platform == 'win32':
        return msvcrt.kbhit()


def inputhook():
    """ Run the pyglet event loop by processing pending events only.

    This keeps processing pending events until stdin is ready.  After
processing
    all pending events, a call to time.sleep is inserted.  This is
needed,
    otherwise, CPU usage is at 100%.  This sleep time should be tuned
though for
    best performance.
    """

    while not stdin_ready():
        pyglet.clock.tick()
        for window in pyglet.app.windows:
            window.switch_to()
            window.dispatch_events()
            window.dispatch_event('on_draw')
            window.flip()
        time.sleep(0.01) # Change this to tune performance
    return 0

callback = CFUNCTYPE(c_int)(inputhook)


def set_inputhook():
    """
    Set PyOS_InputHook for interactive pyglet usage.
    """
    # These must be global or it doesn't work
    global pyos_inputhook_ptr
    global callback
    pyos_inputhook_ptr.value = cast(callback, c_void_p).value


def remove_inputhook():
    """
    Remove the PyOS_InputHook returning it back to its original state.
    """
    pyos_inputhook_ptr.value = orig_pyos_inputhook_ptr_value



window= pyglet.window.Window()
fps_display = pyglet.clock.ClockDisplay()
label = pyglet.text.Label("Hello world !")
label.x = window.width//2 - label.content_width//2
label.y = window.height//2 - label.content_height//2

@window.event
def on_draw():
    window.clear()
    fps_display.draw()
    label.draw()


@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.ESCAPE:
        window.set_visible(False)
        remove_inputhook()
    return

set_inputhook()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to