# ControlP5 toggle in processing.py

**URL:** <https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247>\
**Category:** Processing.py\
**Created:** [August 8, 2019, 8:53am UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247 "2019-08-08T08:53:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luno\_T](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Luno\_T](https://discourse.processing.org/u/Luno_T)\
**Post date:** [August 8, 2019, 8:53am UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247/1 "2019-08-08T08:53:21Z")

</div>

Hello,

I am trying to learn to use the ControlP5 library in python mode. I am struggling with basic stuff as I have no experience in Java, and I can hardly find proper examples for the python mode.

For instance, when I try to run the following code, the program never goes trough the toogle event function:

```auto
add_library("controlP5")

def setup():
    global cp5
    cp5=ControlP5(this)
    size(640,480)
    
    cp5.addToggle("toogle")\
    .setPosition(10,10)\
    .setSize(50,20)\
    .setValue(True)\
    .setMode(ControlP5.SWITCH)\

def draw():
    background(0)
    ellipse (70,20,15,15)
    

def toogle():
    print 'a'

```

Can you help please?

Best wishes,

Luno

---

<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 8, 2019, 9:49am UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247/2 "2019-08-08T09:49:23Z")

</div>

Hi,

I found that it is necessary to add when the ‘toogle’ function will be called.  
So, try add:

`.onClick(toogle)`

in your controlP5 code, and also add an input in the function, because it will ask 1 argument as input. You can try:

`def toogle(the Value):`

So, the code will be like this.

```auto
add_library("controlP5")

def setup():
    global cp5
    cp5=ControlP5(this)
    size(640,480)
    
    cp5.addToggle("toogle")\
    .setPosition(10,10)\
    .setSize(50,20)\
    .setValue(True)\
    .setMode(ControlP5.SWITCH)\
    .onClick(toogle)

def draw():
    background(0)
    ellipse (70,20,15,15)
    

def toogle(theValue):
    print 'a'

```

I am still newbie here. But, I hope it can help to solve your problem.

---

<div class="post-metadata">

**Author:** ![Luno\_T](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Luno\_T](https://discourse.processing.org/u/Luno_T)\
**Post date:** [August 8, 2019, 10:18am UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247/3 "2019-08-08T10:18:56Z")

</div>

Dear Rumidita,

Thank you for your reply. The code runs properly now.

Do you have suggestions of references to begin with controlP5 in python mode?

All the best,

Luno

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [August 8, 2019, 9:27pm UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247/4 "2019-08-08T21:27:30Z")

</div>

I’ve created this basic [ControlP5 Python mode tutorial](https://tabreturn.github.io/code/processing/python/2019/03/16/processing.py_in_ten_lessons-07-_mouse_and_keyboard_interaction.html#controlp5).

Once you understand the fundamental differences of using ControlP5 in Python mode, translating [the Java examples](https://github.com/sojamo/controlp5/tree/master/examples) shouldn’t be too difficult.

---

<div class="post-metadata">

**Author:** ![Luno\_T](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Luno\_T](https://discourse.processing.org/u/Luno_T)\
**Post date:** [August 9, 2019, 6:41am UTC](https://discourse.processing.org/t/controlp5-toggle-in-processing-py/13247/5 "2019-08-09T06:41:01Z")

</div>

Dear tabreturn,

Many thanks for your tutorial. It is definitely what is needed to get started.

Best,

Luno
