# How to update data using onClick button

**URL:** <https://discourse.processing.org/t/how-to-update-data-using-onclick-button/13644>\
**Category:** Processing.py\
**Created:** [August 27, 2019, 9:41am UTC](https://discourse.processing.org/t/how-to-update-data-using-onclick-button/13644 "2019-08-27T09:41:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rumidita](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rumidita/32/6042_2.png) [@rumidita](https://discourse.processing.org/u/rumidita)\
**Post date:** [August 27, 2019, 9:41am UTC](https://discourse.processing.org/t/how-to-update-data-using-onclick-button/13644/1 "2019-08-27T09:41:03Z")

</div>

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.

```auto
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. 🙏🏻

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 27, 2019, 1:21pm UTC](https://discourse.processing.org/t/how-to-update-data-using-onclick-button/13644/2 "2019-08-27T13:21:24Z")

</div>

> [@rumidita](#):
>
> But, it seems still hold the ‘old’ data.

> **[Globals \\ Language (API)](https://py.processing.org/reference/globals.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

```auto
def updateData(v, TXT='new'):
    global data
    data = TXT

```

---

<div class="post-metadata">

**Author:** ![rumidita](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rumidita/32/6042_2.png) [@rumidita](https://discourse.processing.org/u/rumidita)\
**Post date:** [August 28, 2019, 4:52am UTC](https://discourse.processing.org/t/how-to-update-data-using-onclick-button/13644/3 "2019-08-28T04:52:31Z")

</div>

Thank you to point out that the problem is the global variable.  
It really helps. 👍🏻
