Debian Codegraph @

traitsui/examples/demo/Standard_Editors/TextEditor_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 Edit a string, password, or integer
13
14 The TextEditor displays a Str, Password, or Int trait for the user to edit.
15
16 The demo shows all styles of the editor for each of the traits, however certain
17 styles are more useful than others:
18
19 - When editing a Str, consider styles 'simple' (one-line), 'custom'
20 (multi-line), or 'readonly' (multi-line).
21 - When editing a Password, style 'simple' is recommended (shows asterisks).
22 - When editing an Int, consider styles 'simple' and 'readonly'.
23
24 Please refer to the `TextEditor API docs`_ for further information.
25
26 .. _TextEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.text_editor.html#traitsui.editors.text_editor.TextEditor
27 """
28
29 from traits.api import HasTraits, Str, Int, Password
30
31 from traitsui.api import Item, Group, View
32
33
34 # The main demo class:
35 class TextEditorDemo(HasTraits):
36 """Defines the TextEditor demo class."""
37
38 # Define a trait for each of three TextEditor variants:
39 string_trait = Str("sample string")
40 int_trait = Int(1)
41 password = Password()
42
43 # TextEditor display with multi-line capability (for a string):
44 text_str_group = Group(
45 Item('string_trait', style='simple', label='Simple'),
46 Item('_'),
47 Item('string_trait', style='custom', label='Custom'),
48 Item('_'),
49 Item('string_trait', style='text', label='Text'),
50 Item('_'),
51 Item('string_trait', style='readonly', label='ReadOnly'),
52 label='String',
53 )
54
55 # TextEditor display without multi-line capability (for an integer):
56 text_int_group = Group(
57 Item('int_trait', style='simple', label='Simple', id="simple_int"),
58 Item('_'),
59 Item('int_trait', style='custom', label='Custom', id="custom_int"),
60 Item('_'),
61 Item('int_trait', style='text', label='Text', id="text_int"),
62 Item('_'),
63 Item(
64 'int_trait',
65 style='readonly',
66 label='ReadOnly',
67 id="readonly_int",
68 ),
69 label='Integer',
70 )
71
72 # TextEditor display with secret typing capability (for Password traits):
73 text_pass_group = Group(
74 Item('password', style='simple', label='Simple'),
75 Item('_'),
76 Item('password', style='custom', label='Custom'),
77 Item('_'),
78 Item('password', style='text', label='Text'),
79 Item('_'),
80 Item('password', style='readonly', label='ReadOnly'),
81 label='Password',
82 )
83
84 # The view includes one group per data type. These will be displayed
85 # on separate tabbed panels:
86 traits_view = View(
87 text_str_group,
88 text_pass_group,
89 text_int_group,
90 title='TextEditor',
91 buttons=['OK'],
92 )
93
94
95 # Create the demo:
96 demo = TextEditorDemo()
97
98 # Run the demo (if invoked from the command line):
99 if __name__ == "__main__":
100 demo.configure_traits()