Debian Codegraph @

traitsui/examples/demo/Standard_Editors/TitleEditor_demo.py

raw
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """
12 Demonstrates the use of the TitleEditor.
13
14 A TitleEditor can be used to dynamically label sections of a user interface.
15 The text displayed by the TitleEditor is specified by a trait associated with
16 the view.
17
18 This demonstration shows three variations of using a TitleEditor:
19
20 * In the first example, the TitleEditor values are supplied by an Enum trait.
21 Simply select a new value for the title from the drop-down list to cause the
22 title to change.
23 * In the second example, the TitleEditor values are supplied by a Str trait.
24 Simply type a new value into the title field to cause the title to change.
25 * In the third example, the TitleEditor values are supplied by a Property
26 whose value is derived from a calculation on a Float trait. Type a number
27 into the value field to cause the title to changed.
28
29 Please refer to the `TitleEditor API docs`_ for further information.
30
31 .. _TitleEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.title_editor.html#traitsui.editors.title_editor.TitleEditor
32 """
33
34 from traits.api import HasTraits, Enum, Str, Float, Property, cached_property
35
36 from traitsui.api import View, VGroup, HGroup, Item, TitleEditor
37
38
39 class TitleEditorDemo(HasTraits):
40
41 # Define the selection of titles that can be displayed:
42 title = Enum(
43 'Select a new title from the drop down list below',
44 'This is the TitleEditor demonstration',
45 'Acme Widgets Sales for Each Quarter',
46 'This is Not Intended to be a Real Application',
47 )
48
49 # A user settable version of the title:
50 title_2 = Str('Type into the text field below to change this title')
51
52 # A title driven by the result of a calculation:
53 title_3 = Property(observe='value')
54
55 # The number used to drive the calculation:
56 value = Float()
57
58 # Define the test view:
59 traits_view = View(
60 VGroup(
61 VGroup(
62 HGroup(
63 Item(
64 'title',
65 show_label=False,
66 springy=True,
67 editor=TitleEditor(),
68 )
69 ),
70 Item('title'),
71 show_border=True,
72 ),
73 VGroup(
74 HGroup(
75 Item(
76 'title_2',
77 show_label=False,
78 springy=True,
79 editor=TitleEditor(),
80 )
81 ),
82 Item('title_2', label='Title'),
83 show_border=True,
84 ),
85 VGroup(
86 HGroup(
87 Item(
88 'title_3',
89 show_label=False,
90 springy=True,
91 editor=TitleEditor(),
92 )
93 ),
94 Item('value'),
95 show_border=True,
96 ),
97 ),
98 width=0.4,
99 )
100
101 # -- Property Implementations ---------------------------------------------
102
103 @cached_property
104 def _get_title_3(self):
105 if self.value >= 0:
106 return 'The square root of {} is {}'.format(
107 self.value, self.value ** 0.5
108 )
109 else:
110 return 'The square root of {} is {}i'.format(
111 self.value, (-self.value) ** 0.5
112 )
113
114
115 # Create the demo:
116 demo = TitleEditorDemo()
117
118 # Run the demo (if invoked from the command line):
119 if __name__ == '__main__':
120 demo.configure_traits()