Thanks for the reply. I actually figured out away to do this by
looking at the Timer example which is a bit different from yours. I'm
new to Python (and programming) and so I don't know what's the better
approach. I wasn't aware of the %s technique though which should prove
useful later on. My code is below:
import pyglet
from pyglet.window import key
window = pyglet.window.Window(fullscreen=True)
class KeyLabel(object):
def __init__(self):
self.label = pyglet.text.Label('Key', font_size=72,
x=window.width//2,
y=window.height//2,
anchor_x='center',
anchor_y='center')
def setLabel(self):
self.label.text = self.keypressed
@window.event
def on_key_press(symbol, modifiers):
keylabel.keypressed = key.symbol_string(symbol)
keylabel.setLabel()
@window.event
def on_draw():
window.clear()
keylabel.label.draw()
keylabel = KeyLabel()
pyglet.app.run()
Side note: I think there is a mistake in the example in the
documentation which shows BACKSPACE as a parameter which isn't an
integer.
On Jan 30, 3:48 pm, Will <[email protected]> wrote:
> 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.