Skip to content

Instantly share code, notes, and snippets.

@jason-s
Last active December 31, 2015 01:29
Show Gist options
  • Save jason-s/a66731b44cf042c8e6ca to your computer and use it in GitHub Desktop.
Save jason-s/a66731b44cf042c8e6ca to your computer and use it in GitHub Desktop.
test case for enaml
import sys
import enaml
from enaml.qt.qt_application import QtApplication
from atom.api import Atom, Bool
from PySide.QtCore import (Qt)
class P1P2Model(Atom):
p1 = Bool()
p2 = Bool()
class P1Observer(object):
def __init__(self, model):
self.model = model
model.observe('p1', self.onP1Change)
def onP1Change(self, change):
p1old = self.model.p1
p2old = self.model.p2
print "P1Observer sees p1 = %s, sets p2 from %s -> %s" % (p1old, p2old, p1old)
self.model.p2 = p1old
def printchange(change):
print "Event occurred: %s" % change
if __name__ == '__main__':
viewfirst = len(sys.argv) > 1 and sys.argv[1] == 'viewfirst'
with enaml.imports():
from buttontest2view import Main as MainView
app = QtApplication()
model = P1P2Model()
model.observe('p1', printchange)
model.observe('p2', printchange)
if viewfirst:
view = MainView(model=model)
view.show()
obs = P1Observer(model)
else:
obs = P1Observer(model)
view = MainView(model=model)
view.show()
app.start()
from enaml.core.api import Include
from enaml.widgets.api import (
Window, Container, PushButton, Label
)
from atom.api import Atom, Bool, Member
def bequal(p1, p2):
print "enamlview Main sees a change: p1=%s p2=%s" % (p1,p2)
return (p1 and p2) or (not p1 and not p2)
enamldef Main(Window): win:
attr model
Container:
PushButton: go:
text << 'stop' if model.p1 else 'go'
clicked :: model.p1 = not model.p2
enabled << bequal(model.p1, model.p2)
Label: p1:
text << 'p1' if model.p1 else ''
Label: p2:
text << 'p2' if model.p2 else ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment