Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using traits as a model #66

Merged
merged 1 commit into from
Oct 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/traits_model/person.enaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#----------------------------------------------------------------------------
#
# Copyright (c) 2013-14, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in /LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
#----------------------------------------------------------------------------
from enaml.widgets.api import Window, Container, Label, Field, Form
from enaml.stdlib.fields import IntField

enamldef PersonView(Window):
attr person

Container:
Form:
Label:
text = "First Name"
Field:
text := person.first_name
Label:
text = "Last Name"
Field:
text := person.last_name
Label:
text = "Age"
IntField:
minimum = 0
value := person.age
51 changes: 51 additions & 0 deletions examples/traits_model/person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#----------------------------------------------------------------------------
#
# Copyright (c) 2013-14, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in /LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
#----------------------------------------------------------------------------
import enaml
from enaml.qt.qt_application import QtApplication

from traits.api import HasTraits, Str, Range
from traitsui.api import View



class Person(HasTraits):
""" A simple class representing a person object.

"""
last_name = Str()

first_name = Str()

age = Range(low=0)

def _age_changed(self, new):
""" Prints out a message whenever the person's age changes. """
msg = "{first} {last} is {age} years old."
s = msg.format(
first=self.first_name, last=self.last_name, age=self.age,
)
print s

if __name__ == '__main__':
import traits_enaml
with traits_enaml.imports():
from person import PersonView

john = Person(first_name='John', last_name='Doe', age=42)

app = QtApplication()
view = PersonView(person=john)
view.show()

app.start()