# Guido cannot work in python mode

**URL:** <https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501>\
**Category:** Processing.py\
**Created:** [March 21, 2019, 3:48am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501 "2019-03-21T03:48:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fish](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fish](https://discourse.processing.org/u/fish)\
**Post date:** [March 21, 2019, 3:48am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/1 "2019-03-21T03:48:05Z")

</div>

I translated guido exmple into python mode,but it cannot work,thanks for help

```auto
add_library('Guido')

def setup ():
    global slider
    size(400, 400);
     
    Interactive.make( this )
       
    slider = Slider( 2, 2, width-4, 16 )

def draw ():
    global slider
    background( 0 );
    
    fill( 255 - (slider.value * 255) )
    ellipse( width/2, height/2, 150, 150 )
    fill( slider.value * 255 )
    ellipse( width/2, height/2, 70, 70 )

class Slider():

    
    def __init__ ( self, xx, yy, ww, hh ) :
        
        self.x = xx
        self.y = yy
        self.width = ww
        self.height = hh
        
        self.valueX = self.x
        self.value=0

        Interactive.add( this )
    
    

    def mouseDragged (self, mx, my ):
    
        self.valueX = mx - height/2;
        
        if ( self.valueX < self.x ):
            self.valueX = self.x
        if ( self.valueX > self.x+self.width-self.height ):
            self.valueX = self.x+self.width-self.height
        
        self.value = self.map( self.valueX, self.x, self.x+self.width-self.height, 0, 1 );
    

    def draw (self): 
    
        noStroke()
        
        fill( 100 )
        rect(self.x, self.y, self.width, self.height)
        
        fill( 120 );
        rect( self.valueX, self.y, slef.height, self.height )

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 24, 2019, 10:31pm UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/2 "2019-03-24T22:31:14Z")

</div>

Do you have the library installed? Did you get it from Contributions Manager, or from [https://github.com/fjenett/Guido](https://github.com/fjenett/Guido) ?

Does the example work for you in Java mode? In python mode, are you getting error messages? What happens?

Not all Java libraries are compatible with Python mode, although some can be made to work or worked-around…

---

<div class="post-metadata">

**Author:** ![fish](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fish](https://discourse.processing.org/u/fish)\
**Post date:** [March 25, 2019, 11:56am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/3 "2019-03-25T11:56:11Z")

</div>

thanks.I have installed the Guido,and it can work in JAVA mode.there is no error in python mode ,you can paste the code to the processing and run it .I thought python mode run on jython , it should work well just as on java

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 25, 2019, 3:44pm UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/4 "2019-03-25T15:44:18Z")

</div>

> [@fish](#):
>
> I thought python mode run on jython , it should work well just as on java

For some java libraries I believe that it is an interface issue – libraries that modify fields ( and some forms of callbacks and reflection, I think?) aren’t correctly exposed from jython to the mode due to the architecture. See the FAQ for some discussion.

> Some libraries such as [Ani](http://www.looksgood.de/libraries/Ani/) require you to specify a variable name for animation. Unfortunately they cannot access Python variables directly (and Java’s built in classes are immutable). To solve this problem we instead create a mutable `PrimitiveFloat` object. This object has a field `.value` , which you can use for these purposes. [GitHub - jdf/processing.py: Write Processing sketches in Python](https://github.com/jdf/processing.py)

and the wiki

> Some libraries–those that depend on certain functions being defined in your sketch–will not work. As we become aware of those, we will try to work with the authors of those libraries to make the design compatible with Python Mode, or at least to provide workarounds. [GitHub - jdf/processing.py: Write Processing sketches in Python](https://github.com/jdf/processing.py/wiki)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 25, 2019, 9:37pm UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/5 "2019-03-25T21:37:17Z")

</div>

> [@jeremydouglass](#):
>
> Some libraries – those that depend on certain functions being defined in your sketch – will not work.

For such libraries, see whether they offer an alternative way to pass a custom callback (generally an instance of some functional `interface` provided by the library) besides those callbacks directly defined in the PApplet sketch. 😼

---

<div class="post-metadata">

**Author:** ![pheinberg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pheinberg/32/4277_2.png) [@pheinberg](https://discourse.processing.org/u/pheinberg)\
**Post date:** [March 26, 2019, 4:32pm UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/6 "2019-03-26T16:32:52Z")

</div>

Please file a bug at [https://github.com/jdf/Processing.py-Bugs/issues/new](https://github.com/jdf/Processing.py-Bugs/issues/new)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 26, 2019, 5:23pm UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/7 "2019-03-26T17:23:53Z")

</div>

> [@fish](#):
>
> I translated Guido example into Python Mode,

It’d be gr8 if we could also see the original Java Mode runnable version. 🙈

As a general advice, any conversion attempts become much more achievable when both the original & attempt codes are posted together. 😸

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [March 30, 2019, 2:19am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/8 "2019-03-30T02:19:44Z")

</div>

I checked the Guido GitHub repo. It includes some [python-mode examples](https://github.com/fjenett/Guido/tree/master/examples/mode-python), but these don’t seem to work either.

If you’re simply looking for a GUI library with decent Processing.py support, you may want to try _ControlP5_. If you’re interested, I’ve written a [tutorial on using ControlP5](https://tabreturn.github.io/code/processing/python/2019/03/16/processing.py_in_ten_lessons-07-_mouse_and_keyboard_interaction.html#controlp5).

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 30, 2019, 3:53am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/9 "2019-03-30T03:53:12Z")

</div>

> [@tabreturn](#):
>
> some python-mode examples

Looking at the commit dates, an issue might be that Guido (and the examples) are for PDE 2.x and/or an earlier version of Python mode. So one thing you could try is running Guido and those examples with a Python mode release from 2014…

> **[Releases · jdf/processing.py](https://github.com/jdf/processing.py/releases?after=0405)**
>
> Write Processing sketches in Python. Contribute to jdf/processing.py development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 30, 2019, 6:29am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/10 "2019-03-30T06:29:54Z")

</div>

After some gander at Guido repo, seems like all of its Python examples and workarounds had never been released nor tagged for years! 📅

All the Python Mode examples linked by @tabreturn: 🤩

> **[fjenett/Guido](https://github.com/fjenett/Guido/tree/master/examples/mode-python)**
>
> "GUI do ..." another Processing GUI library. Contribute to fjenett/Guido development by creating an account on GitHub.

Relies on an Interactive.**make()** overloaded signature w/ 2 args: `Interactive.make(this, True)`: 😳

> <https://github.com/fjenett/Guido/blob/master/src/de/bezier/guido/Interactive.java#L94-L100>

However, that overloaded `static` method version exists on ‘master’ only, nowhere else! 😮

So even today, only the Interactive.**make()** w/ 1 parameter is available. 😑

However, w/o it, we have to close Python Mode completely before each re-run! 😱

But fret not! Now that Python Mode allows directly access to any Java member, regardless its access permission, we can simply assign `None` to Interactive._manager_ before calling Interactive.**make()**: 🤪

```python
# Workaround for missing overloaded Interactive.make(PApplet, boolean):
print Interactive.manager
Interactive.manager = None 
Interactive.make(this)

```

It forces Interactive.**make()** to always instantiate a `new` Interactive rather than reusing an existing 1, which is unfortunately buggy on Python Mode. 🐛

There are more problems when using Guido on Python Mode though. 🤐  
Interactive.**add()** doesn’t work on Python classes. 🐍  
Instead, we need to create a class which inherits from ActiveElement: 🧐

```python
# ActiveElement inheritance replaces Interactive.add(Object):
class SimpleButton(ActiveElement):

```

W8! There’s 1 more bug! Which was fixed on ‘master’, but it’s unavailable for us: 🤕

Callback ActiveElement::**mousePressed()** is invoked 2 times! 📲

- 1st as signature ActiveElement::**mousePressed()** w/ no parameters.
- 2nd as signature ActiveElement::**mousePressed(float, float)**, which receives mouse’s current coordinates.

> <https://github.com/fjenett/Guido/blob/master/src/de/bezier/guido/ActiveElement.java>

As you can see, all empty callbacks were commented on ‘master’ version. 🙈  
But they’re very active on the distributed version we use currently. 👾

Java, due to its native overloading dispatch, can pick which 1 to use. ☕  
But in Python, we’ve gotta `def` a callback which accepts all overloaded signatures coming at it! 😬

```python
# *args for both signatures ActiveElement::mousePressed() &
# ActiveElement::mousePressed(float, float):
def mousePressed(self, *args):

```

And b/c it’s called back twice, we need to check which version it was, so we don’t toggle _self.on_ twice as well: 🏁

```python
# Only toggles when overloaded callback signature isn't
# ActiveElement::mousePressed():
if args: self.on ^= True
print args, self.on

```

I believe that’s all for now. As you can see, I’ve fixed the button example, not the slider 1, b/c that’s the 1 I was studying while trying to peruse the Guido source: 🙃

```python
"""
 Simple Button (Guido)
 ported from Java to Python by fjenett (2014-May-25)
 fix & mod for old Guido by GoToLoop (2019-Mar-30)

 Discourse.Processing.org/t/guido-cannot-work-in-python-mode/9501/10
 GitHub.com/fjenett/Guido/blob/master/examples/mode-python/button/button.pyde
"""

add_library('Guido')

def setup():
    size(400, 400)

    # Workaround for missing overloaded Interactive.make(PApplet, boolean):
    print Interactive.manager
    Interactive.manager = None 
    Interactive.make(this)

    dim = SimpleButton.DIM
    d = (width - dim) / dim
    dd = d << 1

    for y in range(dim, height - d, dd):
        for x in range(dim, width - d, dd):
            SimpleButton(x, y, d, d)

def draw(): clear()

# ActiveElement inheritance replaces Interactive.add(Object):
class SimpleButton(ActiveElement):
    DIM, ON, OFF, OUTLINE = 20, 200, 100, -1

    def __init__ (self, x, y, w, h):
        super(SimpleButton, self). __init__ (x, y, w, h)
        self.on = False

    # Variant param *args for both sigs ActiveElement::mousePressed() &
    # ActiveElement::mousePressed(float, float):
    def mousePressed(self, *args):
        # Only toggles when overloaded callback signature isn't
        # ActiveElement::mousePressed():
        if args: self.on ^= True
        print args, self.on

    def draw(self):
        stroke(self.OUTLINE) if self.hover else noStroke()
        fill(self.ON if self.on else self.OFF)
        rect(self.x, self.y, self.width, self.height)

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [March 30, 2019, 7:03am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/11 "2019-03-30T07:03:40Z")

</div>

Excellent, @GoToLoop! Thanks for the thorough explanation.

I tested this out, and can confirm that fixing the [GitHub slider example](https://github.com/fjenett/Guido/blob/master/examples/mode-python/slider/slider.pyde) requires the few adjustments/additions you mentioned – specifically:

```auto
class SimpleSlider(ActiveElement):
    ...
    
    #def mouseDragged(self, mx, my, dx, dy ):
    def mouseDragged(self, *args):
        mx = args[0]
        ...

def setup():
    size( 200, 220 )
    Interactive.manager = None
    #Interactive.make(this, True)
    Interactive.make(this)
    ...

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 30, 2019, 7:14am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/12 "2019-03-30T07:14:00Z")

</div>

@ **mouseDragged()** I’d add an `if len(args) < 4: return` check as its 1st statement in order to avoid processing the callback twice needlessly. 🤓

---

<div class="post-metadata">

**Author:** ![fish](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fish](https://discourse.processing.org/u/fish)\
**Post date:** [March 31, 2019, 2:29am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/13 "2019-03-31T02:29:07Z")

</div>

> [@tabreturn](#):
>
> Interactive.manager = None #Interactive.make(this, True) Interactive.make(this)

thank everyone,espcially Go ToLoop,for your help,I have resolved the problem.

```auto
"""
Slider example
"""

add_library('Guido')

# global slider variable
slider = None

class SimpleSlider(ActiveElement):
    
    def __init__ (self, x, y, w, h ):
        super( SimpleSlider, self ). __init__ (x,y,w,h)
        self.hover = False
        self.valueX = x
        self.value = 0
    
    def mouseDragged(self,mx,my,*p ):
        self.valueX = mx - self.height/2
        
        if ( self.valueX < self.x ):
            self.valueX = self.x
        
        if ( self.valueX > self.x+self.width-self.height ):
            self.valueX = self.x+self.width-self.height
        
        self.value = map( self.valueX, self.x, self.x+self.width-self.height, 0, 1 )
        
    def draw(self):
        noStroke()
        fill( 210 if self.hover else 150 )
        rect( self.x, self.y, self.width, self.height )
        fill( 235 )
        rect( self.valueX, self.y, self.height, self.height )

def setup():
    size( 200, 220 )
    Interactive.manager = None
    #Interactive.make(this, True)
    Interactive.make(this)
    
    global slider
    slider = SimpleSlider( 10, height-20, width-20, 10 )

def draw():
    background( 255/2 )
    
    fill( 255 - (slider.value * 255) )
    ellipse( width/2, height/2, 150, 150 )
    fill( slider.value * 255 )
    ellipse( width/2, height/2, 70, 70 )

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 31, 2019, 2:58am UTC](https://discourse.processing.org/t/guido-cannot-work-in-python-mode/9501/14 "2019-03-31T02:58:36Z")

</div>

> [@fish](#):
>
> `def mouseDragged(self, mx, my, *p):`

Nice variant to `def mouseDragged(self, *args):`! 👍  
But given the parameter _my_ isn’t used, just _mx_, just go w/ `def mouseDragged(self, mx, *_):`. 😉

Also, no need for `self.hover = False` inside ` __init__ ()` though. 🤓  
Parent ActiveElement controls that property by itself. 🎛
