File: advanced_view.rst

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (493 lines) | stat: -rw-r--r-- 19,196 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
.. index:: View; internal, View; external, View; ways of displaying
   pair: View; context
   
.. _advanced-view-concepts:

======================
Advanced View Concepts
======================

The preceding chapters of this Manual give an overview of how to use the View
class to quickly construct a simple window for a single HasTraits object. This
chapter explores a number of more complex techniques that significantly increase
the power and versatility of the View object.

* *Internal Views:* Views can be defined as attributes of a HasTraits class; 
  one class can have multiple views. View attributes can be inherited by 
  subclasses.
* *External Views:* A view can be defined as a module variable, inline as a
  function or method argument, or as an attribute of a :term:`Handler`.
* *Ways of displaying Views:* You can display a View by calling 
  configure_traits() or edit_traits() on a HasTraits object, or by calling the
  ui() method on the View object.
* *View context:* You can pass a context to any of the methods for displaying
  views, which is a dictionary of labels and objects. In the default case, this
  dictionary contains only one object, referenced as 'object', but you can 
  define contexts that contain multiple objects.
* *Include objects:* You can use an Include object as a placeholder for view
  items defined elsewhere.

.. index: View; internal
  
.. _internal-views:

Internal Views
--------------

In the examples thus far, the View objects have been external. That is to say,
they have been defined outside the model (HasTraits object or objects) that they
are used to display. This approach is in keeping with the separation of the two
concepts prescribed by the :term:`MVC` design pattern.

There are cases in which it is useful to define a View within a HasTraits class.
In particular, it can be useful to associate one or more Views with a particular
type of object so that they can be incorporated into other parts of the
application with little or no additional programming. Further, a View that is
defined within a model class is inherited by any subclasses of that class, a
phenomenon called *visual inheritance*.

.. _defining-a-default-view:

Defining a Default View
```````````````````````
.. index:: default view, View; default

It is easy to define a default view for a HasTraits class: simply create a View
attribute called **traits_view** for that class. Consider the following
variation on Example 3:

.. index:: configure_traits(); default view example,  default view; example
.. index:: examples; default view
   
.. _example-5-using-configure-traits-with-a-default-view-object:

.. rubric:: Example 5: Using configure_traits() with a default View object

::

    # default_traits_view.py -- Sample code to demonstrate the use of 
    #                           'traits_view'
    from traits.api import HasTraits, Str, Int
    from traitsui.api import View, Item, Group
    import traitsui
    
    class SimpleEmployee2(HasTraits):
        first_name = Str
        last_name = Str
        department = Str
    
        employee_number = Str
        salary = Int
    
        traits_view = View(Group(Item(name = 'first_name'),
                                 Item(name = 'last_name'),
                                 Item(name = 'department'),
                                 label = 'Personnel profile',
                                 show_border = True))
    
    sam = SimpleEmployee2()
    sam.configure_traits()

In this example, configure_traits() no longer requires a *view* keyword
argument; the **traits_view** attribute is used by default, resulting in the
same display as in Figure 3:

.. figure:: images/ui_for_ex3.jpg
   :alt: User interface showing three fields enclosed in a border
   
   Figure 5: User interface for Example 5
   

It is not strictly necessary to call this View attribute **traits_view**. If
exactly one View attribute is defined for a HasTraits class, that View is always
treated as the default display template for the class. However, if there are
multiple View attributes for the class (as discussed in the next section), if
one is named 'traits_view', it is always used as the default.

.. index:: View; multiple, multiple Views

.. _defining-multiple-views-within-the-model:

Defining Multiple Views Within the Model
````````````````````````````````````````

Sometimes it is useful to have more than one pre-defined view for a model class.
In the case of the SimpleEmployee class, one might want to have both a "public
information" view like the one above and an "all information" view. One can do
this by simply adding a second View attribute:

.. index::
   pair: examples; multiple Views
   
.. _example-6-defining-multiple-view-objects-in-a-hastraits-class:

.. rubric:: Example 6: Defining multiple View objects in a HasTraits class

::

    # multiple_views.py -- Sample code to demonstrate the use of 
    #                      multiple views
    from traits.api import HasTraits, Str, Int
    from traitsui.api import View, Item, Group
    import traitsui
    
    class SimpleEmployee3(HasTraits):
        first_name = Str
        last_name = Str
        department = Str
    
        employee_number = Str
        salary = Int
    
        traits_view = View(Group(Item(name = 'first_name'),
                                 Item(name = 'last_name'),
                                 Item(name = 'department'),
                                 label = 'Personnel profile',
                                 show_border = True))
    
        all_view = View(Group(Item(name = 'first_name'),
                              Item(name = 'last_name'),
                              Item(name = 'department'),
                              Item(name = 'employee_number'),
                              Item(name = 'salary'),
                              label = 'Personnel database ' + 
                                      'entry',
                              show_border = True))
    
    sam = SimpleEmployee3()
    sam.configure_traits()
    sam.configure_traits(view='all_view')

.. index:: traits_view attribute, configure_traits(); view parameter

As before, a simple call to configure_traits() for an object of this class
produces a window based on the default View (**traits_view**). In order to use
the alternate View, use the same syntax as for an external view, except that the
View name is specified in single quotes to indicate that it is associated with
the object rather than being a module-level variable::

    configure_traits(view='all_view').
    
Note that if more than one View is defined for a model class, you must indicate
which one is to be used as the default by naming it ``traits_view``. Otherwise,
TraitsUI gives preference to none of them, and instead tries to construct a
default View, resulting in a simple alphabetized display as described in
:ref:`the-view-and-its-building-blocks`. For this reason, it is usually
preferable to name a model's default View traits_view even if there are no other
Views; otherwise, simply defining additional Views, even if they are never
used, can unexpectedly change the behavior of the GUI.

.. index:: View; external

.. _separating-model-and-view-external-views:

Separating Model and View: External Views
-----------------------------------------

In all the preceding examples in this guide, the concepts of model and view have
remained closely coupled. In some cases the view has been defined in the model
class, as in :ref:`internal-views`; in other cases the configure_traits() method
that produces a window from a View has been called from a HasTraits object.
However, these strategies are simply conveniences; they are not an intrinsic
part of the relationship between model and view in TraitsUI. This section
begins to explore how the TraitsUI package truly supports the separation of
model and view prescribed by the :term:`MVC` design pattern.

An *external* view is one that is defined outside the model classes. In Traits
UI, you can define a named View wherever you can define a variable or class
attribute. [7]_ A View can even be defined in-line as a function or method
argument, for example::

    object.configure_traits(view=View(Group(Item(name='a'),
                                            Item(name='b'),
                                            Item(name='c')))
                                            
However, this approach is apt to obfuscate the code unless the View is very
simple.

:ref:`Example 2 <example-2-using-configure-traits-with-a-view-object>` through
:ref:`Example 4 <example-4-using-a-view-object-with-buttons>` demonstrate
external Views defined as variables. One advantage of this convention is that
the variable name provides an easily accessible "handle" for re-using the View.
This technique does not, however, support visual inheritance.

A powerful alternative is to define a View within the :term:`controller`
(Handler) class that controls the window for that View. [8]_ This technique is
described in :ref:`controlling-the-interface-the-handler`.

.. index:: View; methods for displaying

.. _displaying-a-view:

Displaying a View
-----------------

TraitsUI provides three methods for creating a window or panel from a View
object. The first two, configure_traits() and edit_traits(), are defined on the
HasTraits class, which is a superclass of all Traits-based model classes, as
well as of Handler and its subclasses. The third method, ui(), is defined on the
View class itself.

.. index:: configure_traits(); method

.. _configure-traits:

configure_traits()
``````````````````

The configure_traits() method creates a standalone window for a given View
object, i.e., it does not require an existing GUI to run in. It is therefore
suitable for building command-line functions, as well as providing an accessible
tool for the beginning TraitsUI programmer.

The configure_traits() method also provides options for saving 
:term:`trait attribute` values to and restoring them from a file. Refer to the
*Traits API Reference* for details.

.. index:: edit_traits()

.. _edit-traits:

edit_traits()
`````````````

The edit_traits() method is very similar to configure_traits(), with two major
exceptions. First, it is designed to run from within a larger application whose
GUI is already defined. Second, it does not provide options for saving data to
and restoring data from a file, as it is assumed that these operations are
handled elsewhere in the application.

.. index:: ui()

.. _ui:

ui()
````

The View object includes a method called ui(), which performs the actual
generation of the window or panel from the View for both edit_traits() and
configure_traits(). The ui() method is also available directly through the
TraitsUI API; however, using one of the other two methods is usually
preferable. [9]_

The ui() method has five keyword parameters:

* *kind*
* *context*
* *handler*
* *parent*
* *view_elements*

The first four are identical in form and function to the corresponding arguments
of edit_traits(), except that *context* is not optional; the following section
explains why.

The fifth argument, *view_elements*, is used only in the context of a call to
ui() from a model object method, i.e., from configure_traits() or edit_traits(),
Therefore it is irrelevant in the rare cases when ui() is used directly by
client code. It contains a dictionary of the named :term:`ViewElement` objects
defined for the object whose configure_traits() (or edit_traits()) method was
called..

.. index:: context

.. _the-view-context:

The View Context
----------------

All three of the methods described in :ref:`displaying-a-view` have a *context*
parameter. This parameter can be a single object or a dictionary of
string/object pairs; the object or objects are the model objects whose traits
attributes are to be edited. In general a "context" is a Python dictionary whose
keys are strings; the key strings are used to look up the values. In the case of
the *context* parameter to the ui() method, the dictionary values are objects.
In the special case where only one object is relevant, it can be passed directly
instead of wrapping it in a dictionary.

When the ui() method is called from configure_traits() or edit_traits() on a
HasTraits object, the relevant object is the HasTraits object whose method was
called. For this reason, you do not need to specify the *context* argument in
most calls to configure_traits() or edit_traits(). However, when you call the
ui() method on a View object, you *must* specify the *context* parameter, so
that the ui() method receives references to the objects whose trait attributes
you want to modify.

So, if configure_traits() figures out the relevant context for you, why call
ui() at all? One answer lies in *multi-object Views*.

.. index:: multi-object Views, View; multi-object

.. _multi-object-views:

Multi-Object Views
``````````````````

A multi-object view is any view whose contents depend on multiple "independent"
model objects, i.e., objects that are not attributes of one another. For
example, suppose you are building a real estate listing application, and want to
display a window that shows two properties side by side for a comparison of
price and features. This is straightforward in TraitsUI, as the following
example shows:

.. index:: examples; context, context; examples, examples; multi-object Views
.. index:: multi-object Views; examples
   
.. _example-7-using-a-multi-object-view-with-a-context:

.. rubric:: Example 7: Using a multi-object view with a context

::

    # multi_object_view.py -- Sample code to show multi-object view 
    #                         with context
    
    from traits.api import HasTraits, Str, Int, Bool
    from traitsui.api import View, Group, Item
    
    # Sample class
    class House(HasTraits):
       address = Str
       bedrooms = Int
       pool = Bool
       price = Int
    
    # View object designed to display two objects of class 'House'
    comp_view = View(
        Group(
            Group(
                Item('h1.address', resizable=True),
                Item('h1.bedrooms'),
                Item('h1.pool'),
                Item('h1.price'),
                show_border=True
            ),
            Group(
                Item('h2.address', resizable=True),
                Item('h2.bedrooms'),
                Item('h2.pool'),
                Item('h2.price'),
                show_border=True
            ),
            orientation = 'horizontal'
        ),
        title = 'House Comparison'
    )
    # A pair of houses to demonstrate the View
    house1 = House(address='4743 Dudley Lane', 
                   bedrooms=3, 
                   pool=False, 
                   price=150000)
    house2 = House(address='11604 Autumn Ridge', 
                   bedrooms=3, 
                   pool=True, 
                   price=200000)
    
    # ...And the actual display command
    house1.configure_traits(view=comp_view, context={'h1':house1,  
                                                     'h2':house2})
                                                     
.. FIXME: This is a bit assymmetrical. Can we clean it up without complicating
   the example overly?

The resulting window has the desired appearance: [10]_

.. figure:: images/ui_for_ex7.jpg
   :alt: UI showing side-by-side groups.
         
   Figure 6: User interface for Example 7
    
For the purposes of this particular example, it makes sense to create a separate
Group for each model object, and to use two model objects of the same class.
Note, however, that neither is a requirement.

.. index:: extended trait names; Item name attribute

Notice that the Item definitions in Example 7 use the same type of extended
trait attribute syntax as is supported for the on_trait_change() dynamic trait
change notification method. In fact, Item **name** attributes can reference any
trait attribute that is reachable from an object in the context. This is true
regardless of whether the context contains a single object or multiple objects.
For example::

    Item('object.axle.chassis.serial_number')

Because an Item can refer only to a single trait, do not use extended trait
references that refer to multiple traits, since the behavior of such references
is not defined. Also, avoid extended trait references where one of the
intermediate objects could be None, because there is no way to obtain a valid
reference from None.

Refer to the `Traits User Manual <http://github.enthought.com/traits/index.html>`_, in the chapter on trait
notification, for details of the extended trait name syntax.

.. index:: 
   object: Include

.. _include-objects:

Include Objects
---------------
   
In addition to the Item and Group class, a third building block class for Views
exists in TraitsUI: the Include class. For the sake of completeness, this
section gives a brief description of Include objects and their purpose and
usage. However, they are not commonly used as of this writing, and should be
considered unsupported pending redesign.

In essence, an Include object is a placeholder for a named Group or Item object
that is specified outside the Group or View in which it appears. For example,
the following two definitions, taken together, are equivalent to the third:

.. index:: 
   pair: examples; Include
   
.. _example-8-using-an-include-object:

.. rubric:: Example 8: Using an Include object

::

    # This fragment...
    my_view = View(Group(Item('a'),
                         Item('b')),
                   Include('my_group'))
    
    # ...plus this fragment...
    my_group = Group(Item('c'),
                     Item('d'),
                     Item('e'))
    
    #...are equivalent to this:
    my_view = View(Group(Item('a'),
                         Item('b')),
                   Group(Item('c'),
                         Item('d'),
                         Item('e'))
                         
This opens an interesting possibility when a View is part of a model class: any
Include objects belonging to that View can be defined differently for different
instances or subclasses of that class. This technique is called *view
parameterization*.

.. rubric:: Footnotes

.. [7] Note that although the definition of a View within a HasTraits class has
   the syntax of a trait attribute definition, the resulting View is not stored 
   as an attribute of the class.
   
.. [8] Assuming there is one; not all GUIs require an explicitly defined
   Handler.
   
.. [9] One possible exception is the case where a View object is defined as a
   variable (i.e., outside any class) or within a custom Handler, and is 
   associated more or less equally with multiple model objects; see 
   :ref:`multi-object-views`.

.. [10] If the script were designed to run within an existing GUI, it would make
   sense to replace the last line with 
   ``comp_view.ui(context={'h1': house1, 'h2': house2})``, since neither object
   particularly dominates the view. However, the examples in this Manual are
   designed to be fully executable from the Python command line, which is why 
   configure_traits() was used instead.