1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# ------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ------------------------------------------------------------------------------
from __future__ import print_function
import datetime
from atom.api import Atom, Str, Range, Bool, Value, Int, Tuple, observe
import enaml
from enaml.qt.qt_application import QtApplication
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
class Person(Atom):
"""A simple class representing a person object."""
last_name = Str()
first_name = Str()
age = Range(low=0)
dob = Value(datetime.date(1970, 1, 1))
debug = Bool(False)
@observe("age")
def debug_print(self, change):
"""Prints out a debug message whenever the person's age changes."""
if self.debug:
templ = "{first} {last} is {age} years old."
s = templ.format(
first=self.first_name,
last=self.last_name,
age=self.age,
)
print(s)
@observe("dob")
def update_age(self, change):
"""Update the person's age whenever their date of birth changes"""
# grab the current date time
now = datetime.datetime.now()
# estimate the person's age within one year accuracy
age = now.year - self.dob.year
# check to see if the current date is before their birthday and
# subtract a year from their age if it is
if (
now.month == self.dob.month and now.day < self.dob.day
) or now.month < self.dob.month:
age -= 1
# set the persons age
self.age = 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 = Value(Employer)
# The employee's phone number as a tuple of 3 ints
phone = Tuple(Int())
# This method will be called automatically by atom when the
# employee's phone number changes
def _observe_phone(self, val):
print("received new phone number for %s: %s" % (self.first_name, val))
def 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,
phone=(555, 555, 5555),
)
# Import our Enaml EmployeeView
with enaml.imports():
from employee_view import EmployeeView
app = QtApplication()
# Create a view and show it.
view = EmployeeView(employee=employee_mary)
view.show()
app.start()
if __name__ == "__main__":
main()
|