Using G4P on

edited April 2017 in Python Mode

Hi, I want to add to my sketch (processing python sketch) handling of text area from library G4P, but it doesn't work. Here's my code:


text_area = None

def setup():
    size(600, 600)
    global text_area
    text_area = GTextArea(this, 80, 20, 290, 300, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE)
    text_area.setPromptText("Please enter some text");

def draw():


  • edited April 2017

    3rd libraries under Python Mode can't find out where their callbacks are by reflection! :-&
    So default handleTextEvents() won't work there w/o heavy hacking! :ar!

    You're gonna need to ask its dev for it.
    However, he doesn't seem fond to fix G4P as you can see below: :-O

    I've tried to convert this Java Mode attempt I've come up w/:

    // 2017-Apr-10
    import g4p_controls.*;
    GTextArea txtArea;
    void setup() {
      size(600, 400);
      txtArea = new GTextArea(this, 80, 20, 
        width - width/4, height - height/4, 
      txtArea.setPromptText("Please enter some text");
      txtArea.addEventHandler(new MyTxtAreaHandler(), "textAreaEvent");
    void draw() {
    static public class MyTxtAreaHandler {
      static public void textAreaEvent(GTextArea txtArea, GEvent ent) {

    To Python Mode:

    # 2017-Apr-10
    def setup():
        size(600, 400)
        global txtArea
        txtArea = GTextArea(this, 80, 20,
                            width - width/4, height - height/4,
                            G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE)
        txtArea.setPromptText('Please enter some text')
        txtArea.addEventHandler(MyTxtAreaHandler(), 'textAreaEvent')
    def draw():
    class MyTxtAreaHandler:
        def textAreaEvent(txtArea, evt):
            print txtArea.getText()

    No success though... =((

  • edited April 2017

    G4P was created a long time before Python mode appeared so we should not be surprised if they don't work well together.

    G4P uses reflection quite extensively in the event handlers and GoToLoop tells us that Python cannot use them so it looks as if using G4P in Python mode is a non-starter.

  • edited April 2017

    @quark, I still think it'd be possible to make G4P more compatible w/ Python Mode.

    For example, even though ControlP5 relies on reflection a lot, it's still possible to use it under Python Mode w/ some extra effort: #:-S

    In the example from the link above, addListener() accepts a ControlListener instance, which wraps up 1 abstract method called controlEvent().

    In Java, those kinda 1-method interfaces are annotated as @FunctionalInterface: ~O)

    The problem w/ your addEventHandler() is that it accepts an extra String w/ the name of the method for reflection rather than have 1 already pre-determined name. :-<

    Seems like you'd have to create some kinda GListener @FunctionalInterface interface w/ 1 abstract method in it, so we can @Override its method and pass it as a handler for G4P's many controllers. >-)

Sign In or Register to comment.