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
|
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.
"""
Demonstrates the use of the TitleEditor.
A TitleEditor can be used to dynamically label sections of a user interface.
The text displayed by the TitleEditor is specified by a trait associated with
the view.
This demonstration shows three variations of using a TitleEditor:
* In the first example, the TitleEditor values are supplied by an Enum trait.
Simply select a new value for the title from the drop-down list to cause the
title to change.
* In the second example, the TitleEditor values are supplied by a Str trait.
Simply type a new value into the title field to cause the title to change.
* In the third example, the TitleEditor values are supplied by a Property
whose value is derived from a calculation on a Float trait. Type a number
into the value field to cause the title to changed.
"""
# Imports:
from traits.api \
import HasTraits, Enum, Str, Float, Property, cached_property
from traitsui.api \
import View, VGroup, HGroup, Item, TitleEditor
class TitleEditorDemo ( HasTraits ):
# Define the selection of titles that can be displayed:
title = Enum(
'Select a new title from the drop down list below',
'This is the TitleEditor demonstration',
'Acme Widgets Sales for Each Quarter',
'This is Not Intended to be a Real Application'
)
# A user settable version of the title:
title_2 = Str( 'Type into the text field below to change this title' )
# A title driven by the result of a calculation:
title_3 = Property( depends_on = 'value' )
# The number used to drive the calculation:
value = Float
# Define the test view:
view = View(
VGroup(
VGroup(
HGroup(
Item( 'title',
show_label = False,
springy = True,
editor = TitleEditor()
)
),
Item( 'title' ),
show_border = True
),
VGroup(
HGroup(
Item( 'title_2',
show_label = False,
springy = True,
editor = TitleEditor()
)
),
Item( 'title_2', label = 'Title' ),
show_border = True
),
VGroup(
HGroup(
Item( 'title_3',
show_label = False,
springy = True,
editor = TitleEditor()
)
),
Item( 'value' ),
show_border = True
)
),
width = 0.4
)
#-- Property Implementations -----------------------------------------------
@cached_property
def _get_title_3 ( self ):
try:
return ('The square root of %s is %s' %
( self.value, self.value ** 0.5 ))
except:
return ('The square root of %s is %si' %
( self.value, (-self.value) ** 0.5 ))
# Create the demo:
demo = TitleEditorDemo()
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
|