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
|
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.
from traits.api import HasTraits, Str, List
from traitsui.api import Group, Item, TableEditor, View
from traitsui.table_column \
import ObjectColumn
class Word(HasTraits):
word = Str
class Foo(HasTraits):
# arbitrary string containing spaces
input = Str
# input split on space
parsed = List
def _input_changed(self):
words = self.input.split()
for word in self.parsed[:]:
if word.word in words:
words.remove(word.word)
else:
self.parsed.remove(word)
for word in words:
self.parsed.append(Word(word=word))
return
table_editor = TableEditor(
columns = [ ObjectColumn( name='word') ],
editable=True )
help = Str("""Type in the 'input' box before clicking the Parsed tab.
The first non-whitespace character will cause changes to the parsed trait
and therefore changes to the table rows. That is expected.
BUG: the table grabs the focus from 'input' and thus subsequent typing goes
into one of the table cells.
If you click the 'Parsed' tab, to view the table, and then the 'Inputs' tab
the focus will stay with the 'input' box.
""")
traits_view = View(
Group( Item( 'help', style='readonly'),
Item( 'input' ),
label='Input'),
Group( Item( 'parsed', editor=table_editor),
label='Parsed' ),
dock = 'tab',
resizable=True,
width=320,
height=240
)
if __name__ == '__main__':
# simple test of the model
foo = Foo()
foo.input = 'these words in the list'
assert( [word.word for word in foo.parsed] == ['these', 'words', 'in', 'the', 'list'] )
foo.input = 'these dudes in the bar'
assert( [word.word for word in foo.parsed] == ['these', 'in', 'the', 'dudes', 'bar'] )
foo.configure_traits( kind='modal' )
print foo.input, [word.word for word in foo.parsed]
|