This Page

Employee ExampleΒΆ

employee_view.enaml

#------------------------------------------------------------------------------
#  Copyright (c) 2011, Enthought, Inc.
#  All rights reserved.
#------------------------------------------------------------------------------
from enaml.layout.api import vertical, horizontal, align, spacer, vbox
from enaml.widgets.api import (
    Window, Label, Field, Form, DateSelector, CheckBox, GroupBox, Container,
    PushButton
)

from phone_validator import PhoneNumberValidator


enamldef EmployeeForm(Form):
    attr employee
    attr show_employer: bool = True
    Label:
        text = "First name:"
    Field:
        text := employee.first_name
    Label:
        text = "Last name:"
    Field:
        text := employee.last_name
    Label:
        text = "Home phone:"
    Field:
        validator = PhoneNumberValidator()
        text << '(%s) %s-%s' % employee.phone
        text ::
            match = validator.proper.match(text)
            if match:
                area = match.group(1)
                prefix = match.group(2)
                suffix = match.group(3)
                employee.phone = tuple(map(int, (area, prefix, suffix)))
    Label:
        text = 'Date of Birth:'
    DateSelector:
        date := employee.dob
    Label:
        text = 'Age:'
    Label:
        text << str(employee.age)
    Label:
        text = 'Password:'
    Field:
        echo_mode << 'password' if not pw_cb.checked else 'normal'
        text :: print 'Password:', text
    Label:
        text = 'Show Password:'
    CheckBox: pw_cb:
        checked = False
    PushButton:
        checked := show_employer
        checkable = True
        text << ('Hide' if show_employer else 'Show') + ' Employer Details'


enamldef EmployerForm(Form):
    attr employer
    Label:
        text = "Company:"
    Field:
        text << employer.company_name
        enabled << en_cb.checked
    Label:
        text = "Reporting Manager:"
    Field:
        text << "%s %s" % (employer.first_name, employer.last_name)
        enabled << en_cb.checked
    Label:
        text = "Allow Editing:"
    CheckBox: en_cb:
        checked = True


enamldef EmployeeView(Window): main:
    attr employee
    title << "Employee Record for: %s, %s" % (employee.last_name,
                                              employee.first_name)
    Container:
        constraints << [
            vertical(
                top, top_box, btm_box.when(btm_box.visible), spacer, bottom
            ),
            horizontal(left, spacer.flex(), top_box, spacer.flex(), right),
            horizontal(left, spacer.flex(), btm_box, spacer.flex(), right),
            align('midline', top_form, btm_form),
        ]
        GroupBox: top_box:
            share_layout = True
            title = "Personal Details"
            EmployeeForm: top_form:
                share_layout = True
                # We access the employee object through the identifier
                # 'main' here, because the EmployeeForm also has an
                # 'employee' attribute declared, and that would be
                # found first.
                employee = main.employee
        GroupBox: btm_box:
            share_layout = True
            title = "Employer Details"
            visible << top_form.show_employer
            EmployerForm: btm_form:
                share_layout = True
                employer << employee.boss

employee.py

#------------------------------------------------------------------------------
#  Copyright (c) 2011, Enthought, Inc.
#  All rights reserved.
#------------------------------------------------------------------------------
import datetime

from traits.api import HasTraits, Str, Int, Instance, Tuple, Date, Property

import enaml
from enaml.stdlib.sessions import show_simple_view


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

    """
    # The last name of the person as a string
    last_name = Str

    # The first name of the person as a string
    first_name = Str

    # The date of birth of the person
    dob = Date(datetime.date(1970, 1, 1))

    # The age of the person computed from their dob
    age = Property(Int, depends_on='dob')

    # This method is called when the age of the person needs to
    # be computed
    def _get_age(self):
        today = datetime.date.today()
        dob = self.dob
        age = today.year - dob.year
        birthday_this_year = dob.replace(year=today.year)
        if today < birthday_this_year:
            age -= 1
        return age


class Employer(Person):
    """ An employer is a person who runs a company.

    """
    # The name of the company
    company_name = Str


class Employee(Person):
    """ An employee is person with a boss and a phone number.

    """
    # The employee's boss
    boss = Instance(Employer)

    # The employee's phone number as a tuple of 3 ints
    phone = Tuple(Int, Int, Int)

    # This method is called automatically by traits to get the
    # default value for the phone number.
    def _phone_default(self):
        return (555, 555, 5555)

    # This method will be called automatically by traits when the 
    # employee's phone number changes
    def _phone_changed(self, val):
        print 'received new phone number for %s: %s' % (self.first_name, val)


if __name__ == '__main__':
    # Create an employee with a boss
    boss_john = Employer(
        first_name='John', last_name='Paw', company_name="Packrat's Cats"
    )
    employee_mary = Employee(
        first_name='Mary', last_name='Sue', boss=boss_john
    )

    # Import our Enaml EmployeeView
    with enaml.imports():
        from employee_view import EmployeeView

    # Create a view and show it.
    view = EmployeeView(employee=employee_mary)
    show_simple_view(view)
$ python employee.py
../_images/employee.png