~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to docs/source/traitsui_user_manual/view.rst

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. _the-view-and-its-building-blocks:
 
2
 
 
3
================================
 
4
The View and Its Building Blocks
 
5
================================
 
6
 
 
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
 
13
object:
 
14
 
 
15
.. index::
 
16
   pair: examples; configure_traits()
 
17
   
 
18
.. _example-1-using-configure-traits:
 
19
 
 
20
.. rubric:: Example 1: Using configure_traits()
 
21
 
 
22
::
 
23
 
 
24
    # configure_traits.py -- Sample code to demonstrate 
 
25
    #                        configure_traits()
 
26
    from traits.api import HasTraits, Str, Int
 
27
    import traitsui
 
28
    
 
29
    class SimpleEmployee(HasTraits):
 
30
        first_name = Str
 
31
        last_name = Str
 
32
        department = Str
 
33
    
 
34
        employee_number = Str
 
35
        salary = Int
 
36
    
 
37
    sam = SimpleEmployee()
 
38
    sam.configure_traits()    
 
39
 
 
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
 
42
wanted:
 
43
 
 
44
.. figure:: images/ui_for_ex1.jpg
 
45
   :alt: Dialog box showing all attributes of SimpleEmployee in alphabetical order
 
46
   
 
47
   Figure 1: User interface for Example 1
 
48
 
 
49
.. index:: 
 
50
   object: View
 
51
 
 
52
.. _the-view-object:
 
53
 
 
54
The View Object
 
55
---------------
 
56
 
 
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
 
60
panel display.
 
61
 
 
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:
 
67
 
 
68
.. index:: configure_traits(); view parameter, examples; View object
 
69
   
 
70
   
 
71
.. _example-2-using-configure-traits-with-a-view-object:
 
72
 
 
73
.. rubric:: Example 2: Using configure_traits() with a View object
 
74
 
 
75
::
 
76
 
 
77
    # configure_traits_view.py -- Sample code to demonstrate 
 
78
    #                             configure_traits()
 
79
    
 
80
    from traits.api import HasTraits, Str, Int
 
81
    from traitsui.api import View, Item
 
82
    import traitsui
 
83
    
 
84
    class SimpleEmployee(HasTraits):
 
85
        first_name = Str
 
86
        last_name = Str
 
87
        department = Str
 
88
        employee_number = Str
 
89
        salary = Int
 
90
    
 
91
    view1 = View(Item(name = 'first_name'),
 
92
                 Item(name = 'last_name'),
 
93
                 Item(name = 'department'))
 
94
    
 
95
    sam = SimpleEmployee()
 
96
    sam.configure_traits(view=view1)    
 
97
 
 
98
The resulting window has the desired appearance:
 
99
 
 
100
.. figure:: images/ui_for_ex2.jpg
 
101
   :alt: User interface showing only First name, Last name, and Department
 
102
   
 
103
   Figure 2: User interface for Example 2
 
104
 
 
105
A View object can have a variety of attributes, which are set in the View
 
106
definition, following any Group or Item objects.
 
107
 
 
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.
 
111
 
 
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`.
 
115
 
 
116
.. index:: View; contents
 
117
   object: View
 
118
 
 
119
.. _contents-of-a-view:
 
120
 
 
121
Contents of a View
 
122
------------------
 
123
 
 
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
 
129
2.
 
130
 
 
131
The remainder of this chapter describes the Item and Group classes. 
 
132
 
 
133
.. index:: widget, control
 
134
   object: Item
 
135
 
 
136
.. _the-item-object:
 
137
 
 
138
The Item Object
 
139
```````````````
 
140
 
 
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.
 
147
 
 
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.
 
154
 
 
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
 
157
Item class.
 
158
 
 
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
 
170
   
 
171
.. _attributes-of-item-by-category-table:
 
172
 
 
173
.. rubric:: Attributes of Item, by category
 
174
 
 
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.                       |
 
188
|          |* **image**          |                                             |
 
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.)                                     |
 
195
|          |* **springy**        |                                             |
 
196
|          |* **width**          |                                             |
 
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`|
 
210
|          |                     |and                                          |
 
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` |
 
220
|          |                     |).                                           |
 
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
+----------+---------------------+---------------------------------------------+
 
235
 
 
236
.. index:: Label class, Heading class, Spring class
 
237
   pair: Item; subclasses
 
238
 
 
239
.. _subclasses-of-item:
 
240
 
 
241
Subclasses of Item
 
242
``````````````````
 
243
   
 
244
The TraitsUI package defines the following subclasses of Item:
 
245
 
 
246
* Label
 
247
* Heading
 
248
* Spring
 
249
 
 
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*
 
252
for details.
 
253
 
 
254
.. index:
 
255
   object: Group
 
256
 
 
257
.. _the-group-object:
 
258
 
 
259
The Group Object
 
260
````````````````
 
261
   
 
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.
 
269
 
 
270
Consider the following enhancement to Example 2:
 
271
 
 
272
   pair: configure_traits(); examples
 
273
   triple: View; Group; examples
 
274
   
 
275
.. _example-3-using-configure-traits-with-a-view-and-a-group-object:
 
276
 
 
277
.. rubric:: Example 3: Using configure_traits() with a View and a Group object
 
278
 
 
279
::
 
280
 
 
281
    # configure_traits_view_group.py -- Sample code to demonstrate 
 
282
    #                                   configure_traits()
 
283
    from traits.api import HasTraits, Str, Int
 
284
    from traitsui.api import View, Item, Group
 
285
    import traitsui
 
286
    
 
287
    class SimpleEmployee(HasTraits):
 
288
        first_name = Str
 
289
        last_name = Str
 
290
        department = Str
 
291
    
 
292
        employee_number = Str
 
293
        salary = Int
 
294
    
 
295
    view1 = View(Group(Item(name = 'first_name'),
 
296
                       Item(name = 'last_name'),
 
297
                       Item(name = 'department'),
 
298
                       label = 'Personnel profile',
 
299
                       show_border = True))
 
300
    
 
301
    sam = SimpleEmployee()
 
302
    sam.configure_traits(view=view1)    
 
303
 
 
304
The resulting window shows the same widgets as before, but they are now enclosed
 
305
in a visible border with a text label:
 
306
 
 
307
.. figure:: images/ui_for_ex3.jpg
 
308
   :alt: User interface showing three fields enclosed in a border
 
309
   
 
310
   Figure 3: User interface for Example 3
 
311
 
 
312
.. indexx: 
 
313
   pair: contents; Group
 
314
 
 
315
.. _content-of-a-group:
 
316
 
 
317
Content of a Group
 
318
::::::::::::::::::
 
319
   
 
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
 
324
nested to any level.
 
325
 
 
326
.. index:: 
 
327
   pair: attributes; Group
 
328
 
 
329
.. _group-attributes:
 
330
 
 
331
Group Attributes
 
332
::::::::::::::::
 
333
 
 
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. 
 
338
 
 
339
See the *Traits API Reference* for details of the Group class.
 
340
 
 
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
 
349
   
 
350
   
 
351
.. _attributes-of-group-by-category-table:
 
352
 
 
353
.. rubric:: Attributes of Group, by category
 
354
 
 
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**     |                                             |
 
368
|          |* **export**         |                                             |
 
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**    |                                             |
 
376
|          |* **padding**        |                                             |
 
377
|          |* **selected**       |                                             |
 
378
|          |* **show_border**    |                                             |
 
379
|          |* **show_labels**    |                                             |
 
380
|          |* **show_left**      |                                             |
 
381
|          |* **springy**        |                                             |
 
382
|          |* **style**          |                                             |
 
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.                                       |
 
387
|          |                     |                                             |
 
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::         |
 
397
|          |                     |                                             |
 
398
|          |                     |  View(group1)                               |
 
399
|          |                     |                                             |
 
400
|          |                     |The following two do  not::                  |
 
401
|          |                     |                                             |
 
402
|          |                     |  View(group1, group2)                       |
 
403
|          |                     |  View(Group(group1))                        |
 
404
|          |                     |                                             |
 
405
|          |                     |The **help_id** attribute is ignored by the  |
 
406
|          |                     |default help handler, but can be used by a   |
 
407
|          |                     |custom help handler.                         |
 
408
|          |                     |                                             |
 
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
+----------+---------------------+---------------------------------------------+
 
421
 
 
422
.. index::
 
423
   pair: subclasses; Group
 
424
 
 
425
.. _subclasses-of-group:
 
426
 
 
427
Subclasses of Group
 
428
```````````````````
 
429
   
 
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.
 
433
 
 
434
.. index:: HGroup, HFlow, HSplit, Tabbed, VGroup, VFlow, VGrid, VFold, VSplit
 
435
 
 
436
.. _subclasses-of-group_table:
 
437
 
 
438
.. rubric:: Subclasses of Group
 
439
 
 
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     |                                         |
 
467
|           |titles.                       |                                         |
 
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')`                         |
 
474
|           |groups.                       |                                         |
 
475
+-----------+------------------------------+-----------------------------------------+
 
476
 
 
477
 
 
478
.. rubric:: Footnotes
 
479
 
 
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.
 
483
   
 
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`
 
489
   
 
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.