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.
I should note that the global variable this is for the main PApplet canvas. 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.