ControlP5 Radio button event

Hi All,

I tried to make an sketch with some ControlP5 elements (knobs and radio buttons).
Idea is to based on the selected radio button, change specific value with knob.
This I got working.
the harder part is when i change radio button, i would like to set knob to specific (current) value. For that I need to pass event from radio button. However I cannot get this working in python mode.
I took the java example (it is working) and stripped it from what i think is not needed. See the code below.

However I cannot make it work - none of the three controlEvents prints a thing to the console.
What am I doing wrong ?

Any help will be appreciated

Regards
Matt

add_library('controlP5')

def setup():
    size(700,400)
    cp5 = ControlP5(this)
    r = cp5.addRadioButton("radioButton")
    r.setPosition(20,160)
    r.setSize(40,20)
    r.setSpacingRow(50)
    r.addItem("50",1)
    r.addItem("100",2)
        
def draw():
    background(0)
    pass

def controlEvent(theEvent):
    print("got an event from r")
        
# def controlEvent(theEvent):
#     if theEvent.isFrom(r):
#         print("got an event from r")

# def controlEvent(theEvent):
#     if (theEvent.isGroup() and theEvent.name().equals("radioButton")):
#         print(theEvent.arrayValue())
2 Likes

Hi @MattC !

You have to add the “listener” callback function to the cp5 object, like this: cp5.addListener(controlEvent)

add_library('controlP5')

def setup():
    global r
    size(700, 400)
    cp5 = ControlP5(this)
    r = cp5.addRadioButton("radioButton")
    r.setPosition(20, 160)
    r.setSize(40, 20)
    r.setSpacingRow(50)
    r.addItem("50", 1)
    r.addItem("100", 2)
    # adding the listener
    cp5.addListener(controlEvent)     
        
def draw():
    background(0)
    pass

# def controlEvent(theEvent):
#     print("got an event from r")  # worked for me
        
# def controlEvent(theEvent):
#     if theEvent.isFrom(r):
#         print("got an event from r")  # worked for me

def controlEvent(theEvent):
    # Note that in Python strings do not have an "equals" method
    # compare strings with the == operator :)
    if theEvent.isGroup() and theEvent.name() == "radioButton":
        print(theEvent.arrayValue())
3 Likes

Hi @villares,

Thank you for your help. Indeed all three definitions work once the listener is added to the code.
Still need to learn how to properly transform code from java to python.

Kind regards
Matt

1 Like