Traits Ui Slides
Traits Ui Slides
User Interface
Class
David C. Morrill
Enthought, Inc.
[email protected]
What We’re Going To Cover…
View Controller
Control
Model
Defining a Model
• style: A string specifying the default style to use for each item
in the group. As with an Item, the possible values are:
simple, custom, text, readonly. The group’s style value is
only used for items contained in the group that do not
explicitly specify their own style value.
• padding: An integer value (default: 0) in the range from 0 to
15, specifying the amount of extra padding to insert between
each group item as well as around the outside of the group.
• selected: A boolean (default: False) specifying whether the
group should be the selected notebook page when the
containing View is displayed. Obviously, this only applies
when the group represents a page within a notebook, and
only one group at most within the notebook should have a
True value.
Controlling a Group’s Visibility and Status
is equivalent to:
view_elements
view_elements
.simple_editor()
.object Editor
.foo
UIInfo
Toolkit
.ui .control
.info
UI foo
bar
.control
.view wxPython controls
.handler
.ui()
.view_elements
enthought.traits.ui class
What Editors Do
For example, these are the four styles supported by the EnumEditor:
The Standard Trait Editors
For example:
class EMail ( HasTraits ):
msg = Str
spell_check = Button( 'Spell Check' )
view = View(Group(
Group (
Item('msg',
style='custom',
resizable=True),
Item('spell_check'),
show_labels=False)),
height = .3 )
• We can still use the EnumEditor to create the UI, but we’ll
have to provide more information to help it tie things together.
• In this case, we’ll focus on three of the EnumEditor traits that
are of interest for this example:
– values: The values to enumerate (can be a list, tuple, dict,
or a CTrait or TraitHandler than is mapped).
– name: Extended trait name of the trait containing the
enumeration data.
• The values and name traits provide complementary means of
accomplishing the same task: providing the set of
enumeration values independently of the trait being edited.
• In this case, we’ll use the name trait because we have access
to a Stock object whose fruits trait contains the enumeration
of available fruit.
The EnumEditor Editor Factory
• This view relies on two objects: the Order being edited, and a Stock
object containing the available fruits, referred to as ‘stock’ in the
above view.
• This requires providing a context containing both objects when the
Order view is displayed:
order.edit_traits( context = { ‘object’: order, ‘stock’: current_stock,
view = ‘view’ )
The InstanceEditor Editor Factory
For case 2, use the “custom” editor style and ignore the label trait:
class Address ( HasTraits ):
street = Str
city = Str
state = Str
zip = Str
For example:
class Person ( HasTraits ):
name = Str
age = Int
phone = Regex( value = '000-0000',
regex = '\d\d\d[-]\d\d\d\d' )
traits_view = View(
[ [ Item('name'),
Item('_'),
Item( 'captain',
editor = InstanceEditor(
name = 'roster',
editable = False,
values = [ InstanceFactoryChoice(
klass = Person,
name = 'Non player',
view = 'edit_view' ) ] ) ),
Item( '_') ],
[ Item('captain', style='custom') ],
buttons = [ 'OK', 'Cancel' ] )
The InstanceEditor Editor Factory
And here’s an example showing “drag and drop” support:
view = View(
Group(
Group( Item( 'company',
editor = tree_editor,
resizable = True )
show_labels=False),
Group(
Group( Item( label ='Employee of the Month', style = 'custom'),
Item( 'eom',
editor = InstanceEditor( values = [
InstanceDropChoice( klass = Employee,
selectable = True ) ] ),
style='custom', resizable = True )
show_labels = False),
An example of a TableEditor:
The TableEditor Editor Factory
ObjectColumn traits:
• name: Name of the object trait to display/edit
• label: Column label to use for the column
• type: The type of data contained by the column
• text_color: Text color for this column
• text_font: Text font for this column
• cell_color: Cell background color for this column
• read_only_cell_color: Cell background color for non-editable
columns
• horizontal_alignment: Horizontal alignment of text in the column
• vertical_alignment: Vertical alignment of text in the column
• visible: Is the table column visible (i.e. viewable)?
• editable: Is this column editable?
• droppable: Can external objects be dropped on the column?
• editor: Editor factory to use when editing the column “in-place”
• menu: Context menu to display when this column is right-clicked
The TableEditor Editor Factory: Table Columns
The EvalTableFilter:
• Allows a user to enter a Python
expression whose value
determines whether or not an
object meets the filter criteria.
• Its use is obviously best suited
to users already familiar with
Python.
• Trait references on the object
being tested do not need to be
explicitly qualified.
The TableEditor Editor Factory: Table Filters
The RuleTableFilter:
• Allows users to define “rules”
using drop down value entry
for trait names and
operations.
• Rules can be “and”ed or
“or”ed together.
• Rules can be added, deleted
and modified.
• Introspection based…
requires no set-up by the
developer.
The TableEditor Editor Factory: Table Filters
The MenuTableFilter:
• Is similar to the
RuleTableFilter
• The differences are:
– A rule is automatically created
for each object trait.
– Rules cannot be added or
deleted.
– Rules are implicitly “and”ed
together.
– Rules can be turned on and off.
The TableEditor Editor Factory: Table Search
table_editor = TableEditor(
columns = [ ObjectColumn( name = 'name' ),
ObjectColumn( name = 'age' ),
ObjectColumn( name = 'phone' ) ],
deletable = True, sort_model = True, orientation = 'vertical',
edit_view = View( Group( 'name', 'age', 'phone', show_border=True ],
resizable = True ),
filters = [ EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate ],
search = RuleTableFilter(),
row_factory = Employee )
Here are some examples of TreeEditors being used in the VET tool:
The TreeEditor Editor Factory