1
.. _the-view-and-its-building-blocks:
3
================================
4
The View and Its Building Blocks
5
================================
7
A simple way to edit (or simply observe) the attribute values of a
8
:term:`HasTraits` object in a GUI window is to call the object's
9
configure_traits() [3]_ method. This method constructs and displays a window
10
containing editable fields for each of the object's :term:`trait attribute`\ s.
11
For example, the following sample code [4]_ defines the SimpleEmployee class,
12
creates an object of that class, and constructs and displays a GUI for the
16
pair: examples; configure_traits()
18
.. _example-1-using-configure-traits:
20
.. rubric:: Example 1: Using configure_traits()
24
# configure_traits.py -- Sample code to demonstrate
26
from traits.api import HasTraits, Str, Int
29
class SimpleEmployee(HasTraits):
37
sam = SimpleEmployee()
38
sam.configure_traits()
40
Unfortunately, the resulting form simply displays the attributes of the object
41
**sam** in alphabetical order with little formatting, which is seldom what is
44
.. figure:: images/ui_for_ex1.jpg
45
:alt: Dialog box showing all attributes of SimpleEmployee in alphabetical order
47
Figure 1: User interface for Example 1
57
In order to control the layout of the interface, it is necessary to define a
58
View object. A View object is a template for a GUI window or panel. In other
59
words, a View specifies the content and appearance of a TraitsUI window or
62
For example, suppose you want to construct a GUI window that shows only the
63
first three attributes of a SimpleEmployee (e.g., because salary is confidential
64
and the employee number should not be edited). Furthermore, you would like to
65
specify the order in which those fields appear. You can do this by defining a
66
View object and passing it to the configure_traits() method:
68
.. index:: configure_traits(); view parameter, examples; View object
71
.. _example-2-using-configure-traits-with-a-view-object:
73
.. rubric:: Example 2: Using configure_traits() with a View object
77
# configure_traits_view.py -- Sample code to demonstrate
80
from traits.api import HasTraits, Str, Int
81
from traitsui.api import View, Item
84
class SimpleEmployee(HasTraits):
91
view1 = View(Item(name = 'first_name'),
92
Item(name = 'last_name'),
93
Item(name = 'department'))
95
sam = SimpleEmployee()
96
sam.configure_traits(view=view1)
98
The resulting window has the desired appearance:
100
.. figure:: images/ui_for_ex2.jpg
101
:alt: User interface showing only First name, Last name, and Department
103
Figure 2: User interface for Example 2
105
A View object can have a variety of attributes, which are set in the View
106
definition, following any Group or Item objects.
108
The sections on :ref:`contents-of-a-view` through :ref:`advanced-view-concepts`
109
explore the contents and capabilities of Views. Refer to
110
the *Traits API Reference* for details of the View class.
112
Except as noted, all example code uses the configure_traits() method; a detailed
113
description of this and other techniques for creating GUI displays from Views
114
can be found in :ref:`displaying-a-view`.
116
.. index:: View; contents
119
.. _contents-of-a-view:
124
The contents of a View are specified primarily in terms of two basic building
125
blocks: Item objects (which, as suggested by Example 2, correspond roughly to
126
individual trait attributes), and Group objects. A given View definition can
127
contain one or more objects of either of these types, which are specified as
128
arguments to the View constructor, as in the case of the three Items in Example
131
The remainder of this chapter describes the Item and Group classes.
133
.. index:: widget, control
141
The simplest building block of a View is the :term:`Item` object. An Item
142
specifies a single interface :term:`widget`, usually the display for a single
143
trait attribute of a HasTraits object. The content, appearance, and behavior of
144
the widget are controlled by means of the Item object's attributes, which are
145
usually specified as keyword arguments to the Item constructor, as in the case
146
of *name* in Example 2.
148
The remainder of this section describes the attributes of the Item object,
149
grouped by categories of functionality. It is not necessary to understand all of
150
these attributes in order to create useful Items; many of them can usually be
151
left unspecified, as their default values are adequate for most purposes.
152
Indeed, as demonstrated by earlier examples, simply specifying the name of the
153
trait attribute to be displayed is often enough to produce a usable result.
155
The following table lists the attributes of the Item class, organized by
156
functional categories. Refer to the *Traits API Reference* for details on the
159
.. index:: attributes; Item, Item; attributes
160
.. index:: name attribute, dock attribute; Item, emphasized attribute
161
.. index:: export attribute; Item, height attribute; Item, image attribute; Item
162
.. index:: item_theme attribute; Item, label attribute; Item
163
.. index:: label_theme attribute; Item, padding attribute; Item
164
.. index:: resizable attribute, show_label attribute, springy attribute; Item
165
.. index:: width attribute; Item, format_str attribute, format_func attribute
166
.. index:: editor attribute, style attribute; Item, enabled_when attribute; Item
167
.. index:: visible_when attribute; Item, defined_when attribute; Item
168
.. index:: has_focus attribute, tooltip attribute, help attribute; Item
169
.. index:: help_id attribute; Item, id attribute; Item
171
.. _attributes-of-item-by-category-table:
173
.. rubric:: Attributes of Item, by category
175
+----------+---------------------+---------------------------------------------+
176
|Category |Attributes |Description |
177
+==========+=====================+=============================================+
178
|Content | * **name** |These attributes specify the actual data to |
179
| | |be displayed by an item. Because an Item is |
180
| | |essentially a template for displaying a |
181
| | |single trait, its **name** attribute is |
182
| | |nearly always specified. |
183
+----------+---------------------+---------------------------------------------+
184
|Display |* **dock** |In addition to specifying which trait |
185
|format |* **emphasized** |attributes are to be displayed, you might |
186
| |* **export** |need to adjust the format of one or more of |
187
| |* **height** |the resulting widgets. |
189
| |* **item_theme** |If an Item's **label** attribute is specified|
190
| |* **label** |but not its name, the value of **label** is |
191
| |* **label_theme** |displayed as a simple, non-editable string. |
192
| |* **padding** |(This feature can be useful for displaying |
193
| |* **resizable** |comments or instructions in a TraitsUI |
194
| |* **show_label** |window.) |
197
+----------+---------------------+---------------------------------------------+
198
|Content |* **format_str** |In some cases it can be desirable to apply |
199
|format |* **format_func** |special formatting to a widget's contents |
200
| | |rather than to the widget itself. Examples of|
201
| | |such formatting might include rounding a |
202
| | |floating-point value to two decimal places, |
203
| | |or capitalizing all letter characters in a |
204
| | |license plate number. |
205
+----------+---------------------+---------------------------------------------+
206
|Widget |* **editor** |These attributes override the widget that is |
207
|override |* **style** |automatically selected by TraitsUI. These |
208
| | |options are discussed in |
209
| | |:ref:`introduction-to-trait-editor-factories`|
211
| | |:ref:`the-predefined-trait-editor-factories`.|
212
+----------+---------------------+---------------------------------------------+
213
|Visibility|* **enabled_when** |Use these attributes to create a simple form |
214
|and status|* **visible_when** |of a dynamic GUI, which alters the display |
215
| |* **defined_when** |in response to changes in the data it |
216
| |* **has_focus** |contains. More sophisticated dynamic behavior|
217
| | |can be implemented using a custom |
218
| |.. TODO: Add examples|:term:`Handler` see |
219
| | here |:ref:`controlling-the-interface-the-handler` |
221
+----------+---------------------+---------------------------------------------+
222
|User help |* **tooltip** |These attributes provide guidance to the user|
223
| |* **help** |in using the user interface. If the **help** |
224
| |* **help_id** |attribute is not defined for an Item, a |
225
| | |system-generated message is used instead. The|
226
| |.. TODO: Add sample |**help_id** attribute is ignored by the |
227
| | help screen |default help handler, but can be used by a |
228
| | |custom help handler. |
229
+----------+---------------------+---------------------------------------------+
230
|Unique |* **id** |The **id** attribute is used as a key for |
231
|identifier| |saving user preferences about the widget. If |
232
| | |**id** is not specified, the value of the |
233
| | |**name** attribute is used. |
234
+----------+---------------------+---------------------------------------------+
236
.. index:: Label class, Heading class, Spring class
237
pair: Item; subclasses
239
.. _subclasses-of-item:
244
The TraitsUI package defines the following subclasses of Item:
250
These classes are intended to help with the layout of a TraitsUI View, and need
251
not have a trait attribute associated with them. See the *Traits API Reference*
257
.. _the-group-object:
262
The preceding sections have shown how to construct windows that display a simple
263
vertical sequence of widgets using instances of the View and Item classes. For
264
more sophisticated interfaces, though, it is often desirable to treat a group of
265
data elements as a unit for reasons that might be visual (e.g., placing the
266
widgets within a labeled border) or logical (activating or deactivating the
267
widgets in response to a single condition, defining group-level help text). In
268
TraitsUI, such grouping is accomplished by means of the :term:`Group` object.
270
Consider the following enhancement to Example 2:
272
pair: configure_traits(); examples
273
triple: View; Group; examples
275
.. _example-3-using-configure-traits-with-a-view-and-a-group-object:
277
.. rubric:: Example 3: Using configure_traits() with a View and a Group object
281
# configure_traits_view_group.py -- Sample code to demonstrate
283
from traits.api import HasTraits, Str, Int
284
from traitsui.api import View, Item, Group
287
class SimpleEmployee(HasTraits):
292
employee_number = Str
295
view1 = View(Group(Item(name = 'first_name'),
296
Item(name = 'last_name'),
297
Item(name = 'department'),
298
label = 'Personnel profile',
301
sam = SimpleEmployee()
302
sam.configure_traits(view=view1)
304
The resulting window shows the same widgets as before, but they are now enclosed
305
in a visible border with a text label:
307
.. figure:: images/ui_for_ex3.jpg
308
:alt: User interface showing three fields enclosed in a border
310
Figure 3: User interface for Example 3
313
pair: contents; Group
315
.. _content-of-a-group:
320
The content of a Group object is specified exactly like that of a View object.
321
In other words, one or more Item or Group objects are given as arguments to the
322
Group constructor, e.g., the three Items in Example 3. [5]_ The objects
323
contained in a Group are called the *elements* of that Group. Groups can be
327
pair: attributes; Group
329
.. _group-attributes:
334
The following table lists the attributes of the Group class, organized by
335
functional categories. As with Item attributes, many of these attributes can
336
be left unspecified for any given Group, as the default values usually lead to
337
acceptable displays and behavior.
339
See the *Traits API Reference* for details of the Group class.
341
.. index:: object attribute; Group, content attribute; Group
342
.. index:: label attribute; Group, show_border attribute, show_labels attribute
343
.. index:: show_left attribute, padding attribute; Group, layout attribute
344
.. index:: selected attribute, orientation attribute, style attribute; Group
345
.. index:: columns attribute, dock attribute; Group, dock_theme attribute
346
.. index:: group_theme attribute, item_theme attribute; Group
347
.. index:: label_theme attribute; Group, image attribute; Group
348
.. index:: export attribute; Group, springy attribute; Group
351
.. _attributes-of-group-by-category-table:
353
.. rubric:: Attributes of Group, by category
355
+----------+---------------------+---------------------------------------------+
356
|Category |Attributes |Description |
357
+==========+=====================+=============================================+
358
|Content |* **object** |The **object** attribute references the |
359
| |* **content** |object whose traits are being edited by |
360
| | |members of the group; by default this is |
361
| | |'object', but could be another object in the |
362
| | |current context. The **content** attribute is|
363
| | |a list of elements in the group. |
364
+----------+---------------------+---------------------------------------------+
365
|Display |* **columns** |These attributes define display options for |
366
|format |* **dock** |the group as a whole. |
367
| |* **dock_theme** | |
369
| |* **group_theme** |.. index:: enabled_when attribute; Group |
370
| |* **image** |.. index:: visible_when attribute; Group |
371
| |* **item_theme** |.. index:: defined_when attribute; Group |
372
| |* **label** |.. index:: help attribute; Group |
373
| |* **label_theme** |.. index:: help_id attribute; Group |
374
| |* **layout** |.. index:: id attribute; Group |
375
| |* **orientation** | |
377
| |* **selected** | |
378
| |* **show_border** | |
379
| |* **show_labels** | |
380
| |* **show_left** | |
383
+----------+---------------------+---------------------------------------------+
384
|Visibility|* **enabled_when** |These attributes work similarly to the |
385
|and status|* **visible_when** |attributes of the same names on the Item |
386
| |* **defined_when** |class. |
388
| | |.. TODO: Does Item-level or Group-level take |
389
| | | precedence? Find out and document. |
390
+----------+---------------------+---------------------------------------------+
391
|User help |* **help** |The help text is used by the default help |
392
| |* **help_id** |handler only if the group is the only |
393
| | |top-level group for the current View. For |
394
| | |example, suppose help text is defined for a |
395
| | |Group called **group1**. The following View |
396
| | |shows this text in its help window:: |
400
| | |The following two do not:: |
402
| | | View(group1, group2) |
403
| | | View(Group(group1)) |
405
| | |The **help_id** attribute is ignored by the |
406
| | |default help handler, but can be used by a |
407
| | |custom help handler. |
409
| | |.. TODO: The document needs to include |
410
| | | material on organizing Views via Groups, |
411
| | | including the implied top-level group of |
412
| | | every View. If we do this earlier in the |
413
| | | document, it will probably simplify this. |
414
+----------+---------------------+---------------------------------------------+
415
|Unique |* **id** |The **id** attribute is used as a key for |
416
|identifier| |saving user preferences about the widget. If |
417
| | |**id** is not specified, the **id** values |
418
| | |of the elements of the group are concatenated|
419
| | |and used as the group identifier. |
420
+----------+---------------------+---------------------------------------------+
423
pair: subclasses; Group
425
.. _subclasses-of-group:
430
The TraitsUI package defines the following subclasses of Group, which are
431
helpful shorthands for defining certain types of groups. Refer to the *Traits
432
API Reference* for details.
434
.. index:: HGroup, HFlow, HSplit, Tabbed, VGroup, VFlow, VGrid, VFold, VSplit
436
.. _subclasses-of-group_table:
438
.. rubric:: Subclasses of Group
440
+-----------+------------------------------+-----------------------------------------+
441
|Subclass |Description |Equivalent To |
442
+===========+==============================+=========================================+
443
|HGroup |A group whose items are laid |:samp:`Group(orientation='horizontal')` |
444
| |out horizontally. | |
445
+-----------+------------------------------+-----------------------------------------+
446
|HFlow |A horizontal group whose items|:samp:`Group(orientation='horizontal', |
447
| |"wrap" when they exceed the |layout='flow', show_labels=False)` |
448
| |available horizontal space. | |
449
+-----------+------------------------------+-----------------------------------------+
450
|HSplit |A horizontal group with |:samp:`Group(orientation='horizontal', |
451
| |splitter bars to separate it |layout='split')` |
452
| |from other groups. | |
453
+-----------+------------------------------+-----------------------------------------+
454
|Tabbed |A group that is shown as a tab|:samp:`Group(orientation='horizontal' |
455
| |in a notebook. |layout='tabbed', springy=True)` |
456
+-----------+------------------------------+-----------------------------------------+
457
|VGroup |A group whose items are laid |:samp:`Group(orientation='vertical')` |
458
| |out vertically. | |
459
+-----------+------------------------------+-----------------------------------------+
460
|VFlow |A vertical group whose items |:samp:`Group(orientation='vertical', |
461
| |"wrap" when they exceed the |layout='flow', show_labels=False)` |
462
| |available vertical space. | |
463
+-----------+------------------------------+-----------------------------------------+
464
|VFold |A vertical group in which |:samp:`Group(orientation='vertical', |
465
| |items can be collapsed (i.e., |layout='fold', show_labels=False)` |
466
| |folded) by clicking their | |
468
+-----------+------------------------------+-----------------------------------------+
469
|VGrid |A vertical group whose items |:samp:`Group(orientation='vertical', |
470
| |are laid out in two columns. |columns=2)` |
471
+-----------+------------------------------+-----------------------------------------+
472
|VSplit |A vertical group with splitter|:samp:`Group(orientation='vertical', |
473
| |bars to separate it from other|layout='split')` |
475
+-----------+------------------------------+-----------------------------------------+
478
.. rubric:: Footnotes
480
.. [3] If the code is being run from a program that already has a GUI defined,
481
then use edit_traits() instead of configure_traits(). These methods are
482
discussed in more detail in Section 4.3.
484
.. [4] All code examples in this guide that include a file name are also
485
available as examples in the :file:`tutorials/doc_examples/examples`
486
subdirectory of the Traits docs directory. You can run them individually,
487
or view them in a tutorial program by running:
488
:program:`python` :file:`{Traits_dir}/tutorials/tutor.py` :file:`{Traits_dir}/docs/tutorials/doc_examples`
490
.. [5] As with Views, it is possible for a Group to contain objects of more than
491
one type, but it is not recommended.