AH HA!

29 views
Skip to first unread message

Michael Moore

unread,
Sep 13, 2012, 8:57:18 PM9/13/12
to pyjs-...@googlegroups.com
Communicating between classes?

This seems to be a thorny problem in pyjs because some Python structures work...  Some produce unexpected results.

Anyway, I solved the last two problems I posted with some horrid kludge, which pyjs handles very well indeed.

I hacked up the OnClickTest.py Example to this:

from pyjamas.ui.SimplePanel import SimplePanel
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.Button import Button
from pyjamas.ui.RootPanel import RootPanel
from pyjamas import Window
import single

A=single

def onButtonClick(sender):
    Window.alert("function called")

class Object(object):
    pass

class OnClickTest(HorizontalPanel):
   
    def __init__(self):
        HorizontalPanel.__init__(self)
        def localFunc(sender):
            Window.alert("anon object + local func called")
        obj = Object()
        setattr(obj, 'onClick', localFunc)
        self.b = Button("function callback", onButtonClick)
        self.b2 = Button("object callback", self)
        self.b3 = Button("anon object + local func callback", obj)
        A.buttons=[self.b, self.b2, self.b3]

       
        self.add(self.b)
        self.add(self.b2)
        self.add(self.b3)

    def onClick(self, sender):
        Window.alert("object called")

class George(VerticalPanel):
    def __init__(self):
        VerticalPanel.__init__(self)
        self.bx = A.buttons[0]
        self.by = A.buttons[2]
        self.bz = A.buttons[1]
        self.add(self.bx)
        self.add(self.by)
        self.add(self.bz)

def main():
    root=RootPanel()
    v = VerticalPanel()
    keep = OnClickTest()
    v.add(keep)
    g = George()
    v.add(g)
    root.add(v)
    A.buttons[1].setEnabled(False)
   

if __name__ == '__main__':
    main()


It works if you build it, but the three buttons are vertical and the third button down is greyed out.   

I was truly frightened of what something like


class Single(object): 
     """This is a singleton class definition, one instance"""
     _inst = None
     def __new__(cls, *args, **kwargs):
         if cls._inst is not None:
             return cls._inst
         cls._inst = object.__new__(cls, *args, **kwargs)
         return cls._inst


class Borg(object):
    """This can have multiple instances, but all share the same state"""
    _state = {}
    def __new__(cls, *p, **k):
        self = object.__new__(cls)
        self.__dict__ = cls._state
        return self

    def __init__(self, *args, **kw):
        """ Room to code here"""
        pass

might do with pyjs, so I used something much, much simpler.

IN the onclicktest/ directory I did

$ touch single.py

and then imported that.  It works wonderfully as a singleton(global variable keeper).

Michael

Reply all
Reply to author
Forward
0 new messages