Search code examples
pythonpygletpyopengl

Update method in Pyglet


I am new to pyglet and I have a question about the image on the screen update. Necessary each time the mouse pressed to change the count, but I have all the time is zero, where did I go wrong and how can I fix it?

from pyglet.window import mouse
from pyglet import *
import pyglet

window = pyglet.window.Window(800, 600)
n = 0
label = pyglet.text.Label(str(n), x = window.width/2, y = window.width/2 )

@window.event
def on_mouse_press(x, y, button, modifiers):
    if button == mouse.LEFT:
        global n
        n += 1

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

def update():
    pass

pyglet.app.run()
pyglet.clock.schedule_interval(update, 1/30.0)

Thank you!


Solution

  • You need to create again the label in order to change its value (or better modify the label.text attribute)! Actually you didn't pass n to pyglet.text.Label but a string created "on the fly" containing that value.

    @window.event
    def on_mouse_press(x, y, button, modifiers):
        if button == mouse.LEFT:
            global n
            n += 1
            label.text = str(n)
    

    Hope it helps.