| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
# ------------------------------------------------------------------------- |
| 12 |
# Imports: |
| 13 |
# ------------------------------------------------------------------------- |
| 14 |
|
| 15 |
from traits.api import HasStrictTraits, Str, Int, Regex, List |
| 16 |
|
| 17 |
from traitsui.api import View |
| 18 |
|
| 19 |
# ------------------------------------------------------------------------- |
| 20 |
# 'Person' class: |
| 21 |
# ------------------------------------------------------------------------- |
| 22 |
|
| 23 |
|
| 24 |
class Person(HasStrictTraits): |
| 25 |
|
| 26 |
# ------------------------------------------------------------------------- |
| 27 |
# Trait definitions: |
| 28 |
# ------------------------------------------------------------------------- |
| 29 |
|
| 30 |
name = Str() |
| 31 |
age = Int() |
| 32 |
phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') |
| 33 |
|
| 34 |
# ------------------------------------------------------------------------- |
| 35 |
# Traits view definition: |
| 36 |
# ------------------------------------------------------------------------- |
| 37 |
|
| 38 |
traits_view = View( |
| 39 |
'name', |
| 40 |
'age', |
| 41 |
'phone', |
| 42 |
title='Create new person', |
| 43 |
width=0.18, |
| 44 |
buttons=['OK', 'Cancel'], |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
# ------------------------------------------------------------------------- |
| 49 |
# 'WorkingPerson' class |
| 50 |
# ------------------------------------------------------------------------- |
| 51 |
|
| 52 |
|
| 53 |
class WorkingPerson(Person): |
| 54 |
|
| 55 |
# ------------------------------------------------------------------------- |
| 56 |
# Trait definitions: |
| 57 |
# ------------------------------------------------------------------------- |
| 58 |
|
| 59 |
job = Str() |
| 60 |
|
| 61 |
# ------------------------------------------------------------------------- |
| 62 |
# Traits view definition: |
| 63 |
# ------------------------------------------------------------------------- |
| 64 |
|
| 65 |
traits_view = View( |
| 66 |
'name', |
| 67 |
'age', |
| 68 |
'phone', |
| 69 |
'job', |
| 70 |
title='Create new working person.........', |
| 71 |
width=0.18, |
| 72 |
buttons=['OK', 'Cancel'], |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
# ------------------------------------------------------------------------- |
| 77 |
# Sample data: |
| 78 |
# ------------------------------------------------------------------------- |
| 79 |
|
| 80 |
people = [ |
| 81 |
Person(name='Dave', age=39, phone='555-1212'), |
| 82 |
Person(name='Mike', age=28, phone='555-3526'), |
| 83 |
WorkingPerson(name='Joe', age=34, phone='555-6943', job='Fireman'), |
| 84 |
Person(name='Tom', age=22, phone='555-7586'), |
| 85 |
Person(name='Dick', age=63, phone='555-3895'), |
| 86 |
Person(name='Harry', age=46, phone='555-3285'), |
| 87 |
WorkingPerson(name='Sally', age=43, phone='555-8797', job='Soldier'), |
| 88 |
Person(name='Fields', age=31, phone='555-3547'), |
| 89 |
] |
| 90 |
|
| 91 |
# ------------------------------------------------------------------------- |
| 92 |
# 'TableTest' class: |
| 93 |
# ------------------------------------------------------------------------- |
| 94 |
|
| 95 |
|
| 96 |
class TableTest(HasStrictTraits): |
| 97 |
|
| 98 |
# ------------------------------------------------------------------------- |
| 99 |
# Trait definitions: |
| 100 |
# ------------------------------------------------------------------------- |
| 101 |
|
| 102 |
people = List(Person) |
| 103 |
|
| 104 |
# ------------------------------------------------------------------------- |
| 105 |
# Traits view definitions: |
| 106 |
# ------------------------------------------------------------------------- |
| 107 |
|
| 108 |
traits_view = View(['people#', '|<>'], resizable=True) |
| 109 |
|
| 110 |
|
| 111 |
# ------------------------------------------------------------------------- |
| 112 |
# Run the tests: |
| 113 |
# ------------------------------------------------------------------------- |
| 114 |
|
| 115 |
if __name__ == '__main__': |
| 116 |
tt = TableTest(people=people) |
| 117 |
tt.configure_traits() |
| 118 |
for p in tt.people: |
| 119 |
p.print_traits() |
| 120 |
print('--------------') |