File: TitleEditor_demo.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (120 lines) | stat: -rw-r--r-- 3,939 bytes parent folder | download | duplicates (2)
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
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

"""
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.

Please refer to the `TitleEditor API docs`_ for further information.

.. _TitleEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.title_editor.html#traitsui.editors.title_editor.TitleEditor
"""

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(observe='value')

    # The number used to drive the calculation:
    value = Float()

    # Define the test view:
    traits_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):
        if self.value >= 0:
            return 'The square root of {} is {}'.format(
                self.value, self.value ** 0.5
            )
        else:
            return 'The square root of {} is {}i'.format(
                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()