You could just change the text of the label directly when the key is
pressed, something like this:
import pyglet
from pyglet.window import key
from pyglet.window import mouse
window = pyglet.window.Window()
# make a blank label
label = pyglet.text.Label()
@window.event
def on_key_press(symbol, modifiers):
# get the name of the key pressed
message = 'The %s key was pressed' % key.symbol_string(symbol)
# set the text on the label
label.text = message
@window.event
def on_mouse_press(x, y, button, modifiers):
# get the name of the key pressed
message = 'The %s mouse button was pressed' %
mouse.buttons_string(button)
# set the text on the label
label.text = message
@window.event
def on_draw():
window.clear()
label.draw()
pyglet.app.run()
On Jan 29, 12:17 pm, Mohammad <[email protected]> wrote:
> I just started using Pyglet and went through the examples in the
> documentation. In the examples/programming_guide/events.py, the key
> pressed is captured and printed in console. I'm trying to find a way
> to print the key pressed in the window using a label like in
> hello_world.py. The problem is that when I remove the print statements
> and instead set a variable of the keypressed, I cannot access that
> variable outside the function when defining the parameters of the text
> label and therefore cannot draw it in the on_draw event. What would be
> the best way to achieve this?
>
> This is the code from events.py:
>
> import pyglet
> from pyglet.window import key
> from pyglet.window import mouse
>
> window = pyglet.window.Window()
>
> @window.event
> def on_key_press(symbol, modifiers):
> if symbol == key.A:
> print 'The "A" key was pressed.'
> elif symbol == key.LEFT:
> print 'The left arrow key was pressed.'
> elif symbol == key.ENTER:
> print 'The enter key was pressed.'
>
> @window.event
> def on_mouse_press(x, y, button, modifiers):
> if button == mouse.LEFT:
> print 'The left mouse button was pressed.'
>
> @window.event
> def on_draw():
> window.clear()
>
> pyglet.app.run()
--
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.