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

« back to all changes in this revision

Viewing changes to docs/source/traitsui_user_manual/themes.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
.. _traits-ui-themes:
 
2
 
 
3
================
 
4
TraitsUI Themes
 
5
================
 
6
 
 
7
Beginning in Traits 3.0, TraitsUI supports using *themes* to customize the
 
8
appearance of user interfaces, by applying graphical elements extracted from
 
9
simple images. For example, Figure 8 shows an unthemed Traits user interface.
 
10
 
 
11
.. figure:: images/unthemed_ui.jpg
 
12
   :alt: Dialog box with standard widgets
 
13
   
 
14
   Figure 8: Unthemed Traits user interface
 
15
 
 
16
Figure 9 shows the same user interface with a theme applied to it.
 
17
 
 
18
.. figure:: images/themed_ui1.jpg
 
19
   :alt: Dialog box with blue labels and an orange-outlined heading
 
20
   
 
21
   Figure 9: Themed Traits user interface
 
22
 
 
23
Figure 10 shows the same user interface with a different theme applied.
 
24
 
 
25
.. figure:: images/themed_ui2.jpg
 
26
   :alt: Dialog box with tan background and raised widgets and labels
 
27
   
 
28
   Figure 10: Theme Traits user interface with alternate theme
 
29
 
 
30
Theme Data
 
31
----------
 
32
 
 
33
All of the data used by TraitsUI for themes is in the form of simple images, a
 
34
few examples of which are shown in Figure 11:
 
35
 
 
36
.. image:: images/theme_image1.gif
 
37
   :alt: Gray square with rounded, gray, shaded border
 
38
   
 
39
.. image:: images/theme_image2.gif
 
40
   :alt: Yellow square with sharp-cornered, orange, raised border
 
41
   
 
42
.. figure:: images/theme_image3.gif
 
43
   :alt: Green square, raised, with no border
 
44
   
 
45
   Figure 11: Theme images
 
46
 
 
47
Any type of JPEG or Portable Network Graphics (PNG) file is supported. In
 
48
particular, PNG files with alpha information allow smooth compositing of
 
49
multiple theme images. The first image in Figure 11 is an example of a PNG
 
50
file containing alpha information. That is, the interior of the rectangle is not
 
51
gray, but transparent, with a thin alpha gradient shadow around its edges.
 
52
 
 
53
Themeable TraitsUI Elements
 
54
----------------------------
 
55
 
 
56
Theme information can be applied to the following classes of TraitsUI objects:
 
57
 
 
58
- :term:`Group`
 
59
- :term:`Item`
 
60
- :term:`View`
 
61
 
 
62
All of these classes have **item_theme** and **label_theme** attributes, which
 
63
specify the themes for an editor and its label, respectively; the Group class
 
64
also has a **group_theme** attribute, which specifies the theme for the group
 
65
itself. These attributes are defined to be Theme traits, which accept values
 
66
which are either PyFace ImageResource objects, or strings that specify an image
 
67
file to use. In the case of string values, no path information need be included.
 
68
The path to the image file is assumed to be the images subdirectory or
 
69
:file:`images.zip` file located in the same directory as the source file
 
70
containing the string. [13]_ However, if the string begins with an '@'
 
71
(at-sign), the string is assumed to be a reference to an image in the default
 
72
image library provided with PyFace. [14]_
 
73
 
 
74
The **item_theme** and **label_theme** attributes are transferred via
 
75
containment. That is, if an Item object has an **item_theme** defined, that
 
76
value is used for the Item object's editor. If **item_theme** is not defined on
 
77
the Item object, the **item_theme** value from the containing Group is used, and
 
78
so on up to the **item_theme** value on containing View, if necessary.
 
79
Therefore, it is possible to set the item and label themes for a whole user
 
80
interface at the view level.
 
81
 
 
82
The **group_theme** attribute value is not transferred through containment, but
 
83
nested groups automatically visually inherit the theme of the containing group.
 
84
You can, of course, explicitly specify theme information at each level of a
 
85
nested group hierarchy.
 
86
 
 
87
Adding Themes to a UI
 
88
---------------------
 
89
 
 
90
To add themes to a Traits user interface, you add the theme-related attributes
 
91
to the View, Group, and Item definitions. Example 10 shows the code for the
 
92
unthemed user interface shown in Figure 8.
 
93
 
 
94
.. _example-10-traits-ui-without-themes:
 
95
 
 
96
.. rubric:: Example 10: TraitsUI without themes
 
97
 
 
98
.. highlight:: python
 
99
   :linenothreshold: 5
 
100
   
 
101
::
 
102
 
 
103
    # unthemed.py -- Example of a TraitsUI without themes
 
104
    from traits.api import HasTraits, Str, Range, Float, Enum
 
105
    from traitsui.api import View, Group, Item, Label
 
106
    class Test ( HasTraits ):
 
107
    
 
108
        name   = Str
 
109
        age    = Range( 1, 100 )
 
110
        weight = Float
 
111
        gender = Enum( 'Male', 'Female' )
 
112
    
 
113
        view = View(
 
114
            Group(
 
115
                Label( 'An Unthemed Label' ),
 
116
                Item( 'name' ),
 
117
                Item( 'age' ),
 
118
                Item( 'weight' ),
 
119
                Item( 'gender' )
 
120
            ),
 
121
            title   = 'Unthemed TraitsUI',
 
122
        )
 
123
    
 
124
    Test().configure_traits()
 
125
 
 
126
Example 11 shows the code for the user interface shown in Figure 9, which is
 
127
essentially the same as in Example 10, but with theme data added.
 
128
 
 
129
.. _example-11-traits-ui-with-themese:
 
130
 
 
131
.. rubric:: Example 11: TraitsUI with themes
 
132
 
 
133
:: 
 
134
 
 
135
    # themed.py -- Example of a TraitsUI with themes
 
136
    from traits.api import HasTraits, Str, Range, Float, Enum
 
137
    from traitsui.api import View, Group, Item, Label
 
138
    from traitsui.wx.themed_text_editor import \
 
139
        ThemedTextEditor
 
140
    
 
141
    class Test ( HasTraits ):
 
142
    
 
143
        name   = Str
 
144
        age    = Range( 1, 100 )
 
145
        weight = Float
 
146
        gender = Enum( 'Male', 'Female' )
 
147
    
 
148
        view = View(
 
149
            Group(
 
150
                Group(
 
151
                    Label( 'A Themed Label', '@GF6' ),
 
152
                    Item( 'name' ),
 
153
                    Item( 'age' ),
 
154
                    Item( 'weight', editor=ThemedTextEditor()),
 
155
                    Item( 'gender' ),
 
156
                    group_theme = '@GD0'
 
157
                ),
 
158
                group_theme = '@G',
 
159
                item_theme  = '@B0B',
 
160
                label_theme = '@BEA'
 
161
            ),
 
162
            title   = 'Themed TraitsUI',
 
163
        )
 
164
    
 
165
    Test().configure_traits()
 
166
 
 
167
This example uses the following theme-related items:
 
168
 
 
169
- The **group_theme**, **item_theme**, and **label_theme** attributes are 
 
170
  explicitly specified (lines 24 to 26). 
 
171
- The Label constructor (line 17)takes an optional second argument (in this 
 
172
  case '@GF6'), which specifies the **item_theme** information for the Label
 
173
  object. (Label is a subclass of Item.)
 
174
- The item for weight (line 20) uses a ThemedTextEditor factory; this isn't
 
175
  strictly necessary, but illustrates the use of a themed editor factory. For
 
176
  more information on themed editor factories, refer to 
 
177
  :ref:`extra-trait-editor-factories`, and to the *Traits API Reference*.
 
178
- The example contains an extra Group level (line 16), and shows the results of
 
179
  two nested **group_theme** values ('@G' and '@GD0'). The outermost 
 
180
  **group_theme** value ('@G') specifies the gray background, while the 
 
181
  innermost **group_theme** value ('@GD0') specifies the light gray rectangle
 
182
  drawn over it. This combination demonstrates the automatic compositing of 
 
183
  themes, since the rounded rectangle is transparent except where the light 
 
184
  gray band appears.
 
185
- The theme data strings use the '@' prefix to reference images from the 
 
186
  default image library.  
 
187
 
 
188
.. rubric:: Footnotes
 
189
 
 
190
.. [13]  This is very similar to the way that PyFace ImageResource objects work
 
191
   when no search path is specified.
 
192
   
 
193
.. [14] PyFace is provided by the pyface package in the Traits GUI 
 
194
   project (not to be confused with the TraitsUI package, traitsui,
 
195
   the subject of this document.)