Hi,

I noticed a few issues with the code that will cause the message to not
show up:

   1. You need to decorate "on_key_press" with @window.event to make sure
   it can respond to key presses;
   2. There is a typo in anchor_y='centre', that should be center;
   3. "label1" is local to on_key_press and as such is deleted when
   on_key_press ends, you need to keep a reference to the label to prevent it
   from being deleted;
   4. You are not calling draw() on the new label.

I have modified you code a little bit to give a working example:

import pyglet

#showing the main window and the main label
window = pyglet.window.Window(800, 600, "Hi!")
label = pyglet.text.Label('Hello world!',
                          font_name='Arial',
                          font_size=28,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
label1 = None

#defining the color of the background(?)
@window.event
def on_draw():
    window.clear()
    label.draw()
    if label1:
        label1.draw()

#defining what happens when you press a key
@window.event
def on_key_press(symbol, modifiers):
    global label1
    label1 = pyglet.text.Label('A key was pressed',
                               font_name='Arial',
                               font_size=18,
                               x=window.width//2, y=window.height//3,
                               anchor_x='center', anchor_y='center')

pyglet.app.run()

Usually you would define a class to keep the variables for the window, but
I was a bit lazy and just made label1 global ;-).

Rob

On 22 February 2018 at 20:59, Sesgiog <[email protected]> wrote:

> i have this code:
>
> import pyglet
>
> #showing the main window and the main label
> window = pyglet.window.Window(800, 600, "Hi!")
> label = pyglet.text.Label('Hello world!',
>                           font_name='Arial',
>                           font_size=28,
>                           x=window.width//2, y=window.height//2,
>                           anchor_x='center', anchor_y='center')
>
> #defining the color of the background(?)
> @window.event
> def on_draw():
>     window.clear()
>     label.draw()
>
> #defining what happens when you press a key
> def on_key_press(symbol, modifiers):
>     label1 = pyglet.text.Label('A key was pressed',
>                                font_name='Arial',
>                                font_size=18,
>                                x=window.width//2, y=window.height//3,
>                                anchor_x='center', anchor_y='centre')
>
> pyglet.app.run()
>
>
> the idea was to show a text messagge under the 'Hello world' message when
> i press a key,but it not function(excuse me for my bad english,i'm only
> 14),please,can someone help me?
> thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/pyglet-users.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to