We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone! I'm trying to come up with a GUI class in Processing, Python mode, but the code does not work. The error says:
"NameError: global name 'cp5' is not defined"
But I've defined it in the main Sketch... Any suggestions? Here is the code
add_library('controlP5')
from gui import Gui
gui1=None
def setup():
size(600, 600)
cp5 = ControlP5(this)
gui1=Gui()
############################ GUI CLASS ##########################
class Gui(object):
def __init__(self):
################## CONTROLS #################
cp5.addSlider("North St.") \
.setPosition(width/2-10, 20) \
.setSize(20,200) \
.setRange(100,0) \
.setValue(50) \
.getCaptionLabel() \
.align(ControlP5.CENTER, ControlP5.TOP_OUTSIDE) \
.setPaddingX(0)
cp5.addSlider("East St.") \
.setPosition(width-210, height/2-10) \
.setSize(200,20) \
.setRange(100,0) \
.setValue(50) \
.getCaptionLabel() \
.align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0) \
cp5.getController("East St.") \
.getValueLabel() \
.align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0)
cp5.addSlider("South St.") \
.setPosition(width/2-10, height-220) \
.setSize(20,200) \
.setRange(0,100) \
.setValue(50) \
.getCaptionLabel() \
.align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0)
cp5.addSlider("West St.") \
.setPosition(10, height/2-10) \
.setSize(200,20) \
.setRange(0,100) \
.setValue(50) \
.getCaptionLabel() \
.align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0) \
cp5.getController("West St.") \
.getValueLabel() \
.align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0)
cp5.addKnob("Rate") \
.setRange(0, 100) \
.setValue(50) \
.setPosition(width/7, height-150) \
.setRadius(50) \
.setNumberOfTickMarks(10) \
.setTickMarkLength(5) \
.setDragDirection(Knob.VERTICAL)
cp5.addCheckBox("Cross") \
.setPosition(width-150,height-150) \
.setColorForeground(color(120)) \
.setColorActive(color(255)) \
.setColorLabel(color(255)) \
.setSize(100, 100) \
.addItem("CROSS", 0) \
.getController("CROSS") \
.getCaptionLabel() \
.align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
.setPaddingX(0)
############################ BACKGROUND ################################
noFill()
stroke(255)
beginShape()
vertex(10, height/2-50)
vertex(width/2-50, height/2-50)
vertex(width/2-50, 10)
endShape()
beginShape()
vertex(width/2+50, 10)
vertex(width/2+50, height/2-50)
vertex(width-10, height/2-50)
endShape()
beginShape()
vertex(width-10, height/2+50)
vertex(width/2+50, height/2+50)
vertex(width/2+50, height-10)
endShape()
beginShape()
vertex(width/2-50, height-10)
vertex(width/2-50, height/2+50)
vertex(10, height/2+50)
endShape()
###################### ZEBRAS ######################
fill(255)
rectMode(CENTER)
begin = width/2-40
for i_begin in range(10):
transition = i_begin * 9
rect(begin + transition, height/2-65, 5, 20)
begin = width/2-40
for i_begin in range(10):
transition = i_begin * 9
rect(width/2 + 65 , begin + transition, 20, 5)
begin = width/2-40
for i_begin in range(10):
transition = i_begin * 9
rect(begin + transition, height/2+65, 5, 20)
begin = width/2-40
for i_begin in range(10):
transition = i_begin * 9
rect(width/2 - 65, begin + transition, 20, 5)
Thank you!
Answers
Dunno Python! But I believe a variable defined inside a
def
is local to that function only, isn't it? :-?Hi! thanks for your answer, I've tried also like this
but I get a Null Pointer Exception….
No guarantees, since I dunno Python, but try out:
Hi, did you see this python and controlP5 discussion and sample code on processing.py's github page?
Hello Andeas!
I've read the discussion on github but I cannot figure out how to use that solution in my case. If I implement the ControlP5 directly in the main program (without using a class) it work just fine:
The problem shows up just when I try to wrap it in a class to have the code in a different tab... Any idea?
In Java, JavaScript, CoffeeScript & Android modes, all ".pde" tabs are merged as 1 "top-class" file.
Our own classes would then become nested classes from this unified "top-class".
Variables defined at top level, like the cp5 for example, would be readily available to those nested classes too.
Although that'd be frowned upon, since we coulda formally passed any top level variables to a nested class!
Dunno whether Python Mode got nested classes too. Or whether it would work the same as the other modes do.
Rather than wondering whether that's so or not, it's much safer & more formal to pass any needed top-level variables to a class anyways!
Warning: Non-Python programmer ahead: :-&
Right, agree with you!
I've also tried to write it as following, but does not work...
It's strange, I guess it's just a matter of writing it in a separate tab, because if written all in the "main program" it work fine...
@ line #22, you're instantiating another ControlP5 object, rather than using the passed reference!!! :-O
Made some research for Python's class and static & instance variables.
Come up w/ this 1 now. Again, no idea whether it works or not: ~:>
Hey, thank you for your efforts!
If I run the code with the line 4 commented I get this error here:
but if I uncomment the line 4, the error now shows in the Gui Class at line 26 "def createControls():"
Hmm... Why
@ staticmethod
isn't indented to matchdef createControls():
as I posted? :OIf it fails too, remove the space between
@
andstaticmethod
.I've gotta insert a space due to forum's bug! 8-|
Still doesn't work my friend... thanks a lot for yours efforts!
I made it works. https://github.com/PymientoProject/josemaria/tree/master/SuperFormulaPy You can see it.
Now... the function ...
controlEvent(theEvent):
doesn't work