How to use controlP5 in second window using python mode

Hello,

I want to put a button in the second window using ‘bang’ function from controlP5 library.
I managed to do it in the main window, but it fails when I try to put in the second window. Instead of appearing in the second window, the bang button appears in the main window.

Here is my code,

add_library('controlP5') 

def setup():    
    size(300, 300)
    global sa
    global cp5
    sa = SecondApplet()
    switches = '--sketch-path=' + sketchPath(), '' 
    PApplet.runSketch(switches, sa) 
    
def draw():
    background(200)    

class SecondApplet(PApplet):
    global parent
    parent = PApplet    
   
    def settings(p):
        p.size(300, 100), p.smooth(3), p.noLoop()
 
    def setup(p):
        p.width == 100 and p.settings()

        cp5 = ControlP5(this)
        cp5.addBang('OK').plugTo(parent,'OK').setPosition(50,50)\
                         .setSize(60,30)\
                         .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)        
 
    def draw(p):
        p.background(-1), p.fill(0)
        p.text("File is not found", 10, 10)

I tried to adapt from this which is written in Java. To be honest, I am still pretty new with this coding world, so, I do not know excatly how to convert this java to python.

Thanks for your time.

While both in Java & JS this is a keyword, in Python Mode it’s merely another global variable. :open_mouth:

Given SecondApplet is a subclass of PApplet, you need to pass its reference to ControlP5: :bulb:
cp5 = ControlP5(p).

2 Likes

I see. I missed passing the reference to ControlP5. That’s why it appears in the main window.
Now, it works as desired. Thanks.

Python Mode’s global variable this is the reference for the main PApplet canvas. :art:

1 Like

I should note that the global variable this is for the main PApplet canvas. :+1: Thanks

Now, I got another problem.
I want to execute a function when the bang is clicked.
I add .onClick(myFunction) and declare the global function of myFunction. But, it seems nothing happens. I do the same for the controller in the main PApplet canvas, and it works well. So, I don’t get why it cannot work in the subclass.

I write my code as below.

add_library('controlP5') 

def setup():    
    size(300, 300)
    global sa
    global cp5
    sa = SecondApplet()
    switches = '--sketch-path=' + sketchPath(), '' 
    PApplet.runSketch(switches, sa) 
    
def draw():
    background(200)    

class SecondApplet(PApplet):
    global parent
    parent = PApplet    
   
    def settings(p):
        p.size(300, 100), p.smooth(3), p.noLoop()
 
    def setup(p):
        p.width == 100 and p.settings()

        cp5 = ControlP5(p)
        cp5.addBang('OK').plugTo(parent,'OK').setPosition(50,50)\
                         .setSize(60,30)\
                         .onClick(myFunction)\
                         .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)        
 
    def draw(p):
        p.background(-1), p.fill(0)
        p.text("File is not found", 10, 10)

def myFunction(theValue):    
    print('It is OK!')

I wonder if there is something missing that I have to declare or define.

After creating a more simplified example: :sweat_smile:

# https://Discourse.Processing.org/t/
# how-to-using-controlp5-in-second-window-using-python-mode/13253/6

# GoToLoop (2019-Aug-19)

add_library('controlP5')

def settings():
    size(150, 150)
    Other()


def setup():
    ControlP5(this).addBang('Main').onClick(bangCallback)


def draw():
    background(0)


def bangCallback(v):
    print v.controller


class Other(PApplet):
    SW_PATH = '--sketch-path='
    MAGENTA = 0xFFff00ff

    def __init__(p):
        switches = Other.SW_PATH + sketchPath(), ''
        PApplet.runSketch(switches, p)


    def settings(p):
        p.size(150, 150)


    def setup(p):
        ControlP5(p).addBang('Other').onClick(bangCallback)


    def draw(p):
        p.background(Other.MAGENTA)

I’ve just realized the “bug” is due to calling noLoop() within SecondApplet::settings(): :bug:
p.size(300, 100), p.smooth(3), p.noLoop()

3 Likes

Thanks for mentioning this point. This really solves the problem.
:blush: :blush:

The example that you provided is also helped to understand the difference for using the controlP5 library in the main canvas and in the subclass. :star_struck:

2 Likes