How to update data using onClick button

I would like to update data using onClick button, so I try using an item from controlP5 library. I hope when I click the button, the ‘old’ data will be changed by the ‘new’ data. But, it seems still hold the ‘old’ data.

Here is my code.

add_library('controlP5')

def updateData(v):
    data = 'new'

def setup():
    global data
    size(200,200)
    frameRate(5)
    data = 'old'
    cp5 = ControlP5(this)
    cp5.addBang('Update').setPosition(50,50)\
                         .setSize(60,30)\
                         .onClick(updateData)\
                         .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)

def draw():
    print(data)

When I try to run the program, it prints the ‘old’ data every time, even I click on the button.

Hope someone can help. :pray:t2:

2 Likes

Py.Processing.org/reference/globals.html

def updateData(v, TXT='new'):
    global data
    data = TXT
3 Likes

Thank you to point out that the problem is the global variable.
It really helps. :+1:t2:

1 Like