File: TableEditor_demo.py

package info (click to toggle)
python-traitsui 6.1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,252 kB
  • sloc: python: 44,765; makefile: 117
file content (111 lines) | stat: -rw-r--r-- 3,087 bytes parent folder | download
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
#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

"""
Implementation of a TableEditor demo plugin for Traits UI demo program

This demo shows the full behavior of a straightforward TableEditor.  Only
one style of TableEditor is implemented, so that is the one shown.
"""

# Import statements:
from __future__ import absolute_import
from traits.api \
    import HasTraits, HasStrictTraits, Str, Int, Regex, List

from traitsui.api \
    import View, Group, Item, TableEditor

from traitsui.table_column \
    import ObjectColumn, ExpressionColumn

from traitsui.table_filter \
    import EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate, \
    EvalTableFilter

# A helper class for the 'Department' class below:


class Employee(HasTraits):
    first_name = Str
    last_name = Str
    age = Int
    phone = Regex(value='000-0000', regex='\d\d\d[-]\d\d\d\d')

    traits_view = View(
        'first_name', 'last_name', 'age', 'phone',
        title='Create new employee',
        width=0.18,
        buttons=['OK', 'Cancel']
    )

# The definition of the demo TableEditor:
table_editor = TableEditor(
    columns=[ObjectColumn(name='first_name', width=0.20),
             ObjectColumn(name='last_name', width=0.20),
             ExpressionColumn(
        label='Full Name',
        width=0.30,
        expression="'%s %s' % (object.first_name, "
        "object.last_name )"),
        ObjectColumn(name='age', width=0.10,
                     horizontal_alignment='center'),
        ObjectColumn(name='phone', width=0.20)],
    deletable=True,
    sort_model=True,
    auto_size=False,
    orientation='vertical',
    edit_view=View(
        Group('first_name', 'last_name', 'age', 'phone',
                            show_border=True
              ),
        resizable=True
    ),
    filters=[EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate],
    search=EvalTableFilter(),
    show_toolbar=True,
    row_factory=Employee)

# The class to be edited with the TableEditor:


class Department(HasStrictTraits):

    employees = List(Employee)

    traits_view = View(
        Group(
            Item('employees',
                 show_label=False,
                 editor=table_editor
                 ),
            show_border=True,
        ),
        title='TableEditor',
        width=.4,
        height=.4,
        resizable=True,
        buttons=['OK'],
        kind='live'
    )

# Create some employees:
employees = [
    Employee(first_name='Jason', last_name='Smith',
             age=32, phone='555-1111'),
    Employee(first_name='Mike', last_name='Tollan',
             age=34, phone='555-2222'),
    Employee(first_name='Dave', last_name='Richards',
             age=42, phone='555-3333'),
    Employee(first_name='Lyn', last_name='Spitz',
             age=40, phone='555-4444'),
    Employee(first_name='Greg', last_name='Andrews',
             age=45, phone='555-5555')
]

# Create the demo:
demo = Department(employees=employees)

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    demo.configure_traits()