How to active ControlEvent in ControlP5 - python Mode?

in Java language, here is the original code:

import controlP5.*;

ControlP5 cp5;
int myColor = color(0, 0, 0);

int sliderValue = 100;
int sliderTicks1 = 100;
void setup() {
size(700, 400);
noStroke();
cp5 = new ControlP5(this);

cp5.addSlider(“slider”)
.setPosition(100, 305)
.setSize(200, 20)
.setRange(0, 200)
.setValue(128)
;
}

void draw() {
background(0);
}

void slider(float theColor) {
myColor = color(theColor);
println("a slider event. setting background to "+theColor);
}

It work well: When I change the slider, the “void slider” will be activated and print what I want to.

But when I use python to write this, the “def slider” or other ControlEvent will not active. How to make ControlP5 work correctly in python Mode? Here’s the code below:

add_library(‘controlP5’)
def setup():
size(700,400)
noStroke()
global slider
cp5 = ControlP5(this)

slider = cp5.addSlider("slider")
slider.setPosition(100, 305)
slider.setSize(200, 20)
slider.setRange(0, 200)
slider.setValue(128)

def draw():
background(0)

def slider(theColor):
myColor = color(theColor)
print("a slider event.setting background to " + theColor)

I think you’re looking for the .value method:

def setup():
    global slider
    size(700,400, P2D)
        
    cp5 = ControlP5(this)
    slider = cp5.addSlider("slider").setPosition(100, 305).setSize(200, 20).setRange(0, 200).setValue(128)

def draw():
    background(slider.value)
    print slider.value

Thank you solub, actually I want to active a function while slider value changed.
Maybe in JAVA , void ControlEvent can work. But in python mode def ControlEvent will not be avtivated.

Could you please elaborate further on what you’re trying to achieve ? What is that slider function for exactely ?

In JAVA code:

import controlP5.*;
ControlP5 cp5;
Slider slider;
int myColor = color(0, 0, 0);
int sliderValue = 100;
int sliderTicks1 = 100;
void setup() {
size(700, 400);
noStroke();
cp5 = new ControlP5(this);
slider = cp5.addSlider(“slider”)
.setPosition(100, 305)
.setSize(200, 20)
.setRange(0, 200)
.setValue(128)
;
}
void draw() {
background(0);
}

void controlEvent(ControlEvent theEvent){
if (theEvent.isFrom(slider)){
myFunction();
}
}

when I change the slider value, it will activate myFunction(). Actually in myFunction I want to use a slider to control the controlP5 Group’s number in realTime. But nothing happend in python mode:

add_library(‘controlP5’)
def setup():
size(700,400)
noStroke()
global slider
cp5 = ControlP5(this)
slider = cp5.addSlider(“slider”)
slider.setPosition(100, 305)
slider.setSize(200, 20)
slider.setRange(0, 200)

def draw():
background(0)

def controlEvent(theEvent):
global slider
if (theEvent.isFrom(slider)):
myFunction()

"""
 ControlP5::addListener() (v1.1)
 GoToLoop (2018-Jun-20)

 https://Discourse.Processing.org/t/
 how-to-active-controlevent-in-controlp5-python-mode/1113/6
"""

add_library('controlP5')

bg = 0200

def setup():
    size(400, 300)

    global slider
    slider = ControlP5(this).\
             addSlider('slider').\
             setSize(width>>1, 20).\
             setPosition(width>>2, height>>1).\
             setRange(0, 0xff).\
             setValue(bg).\
             addListener(sliderListener)


def draw(): background(bg)


def sliderListener(evt):
    global bg
    bg = int(evt.getValue())

Thank you GoToLoop! That works.
One more step if you don’t mind, what if I want to listen the specific slider for example: Slider01?

And where can I get the information that I can use def sliderListener to listen sliders? I can’t find this function in controlP5 website libraries

Your Java version relies on method isFrom(). So that’s what I did now: :smirk:

"""
 ControlP5::addListener() (v1.2)
 GoToLoop (2018-Jun-20)

 https://Discourse.Processing.org/t/
 how-to-active-controlevent-in-controlp5-python-mode/1113/8
"""

add_library('controlP5')

bg = 0200

def setup():
    size(400, 300)

    global slider
    slider = ControlP5(this).\
             addSlider('slider').\
             setSize(width>>1, 20).\
             setPosition(width>>2, height>>1).\
             setRange(0, 0xff).\
             setValue(bg).\
             addListener(sliderListener)


def draw(): background(bg)


def sliderListener(evt):
    global bg
    if evt.isFrom(slider): bg = int(evt.getValue())

I’ve found that out at “Python Mode Examples”. :smile_cat:
Although I already knew about the class extending ControlListener approach. :triumph:

Hit CTRL+SHIFT+O, then go to “Contributed Libraries in Python” → “ControlP5” → “Textfield”. :face_with_monocle:

I found a new way to achieve that, bro:

Slider01 = ControlP5(this).

addListener(youCanNameThis)

def youCanNameThis(evt):
function()

That’s listen to Slider01 only.

But my sketch already just listens to slider only! :roll_eyes: