Debian Codegraph @

traitsui/examples/demo/Standard_Editors/InstanceEditor_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 **WARNING**
13
14 This demo might not work as expected and some documented features might be
15 missing.
16
17 -------------------------------------------------------------------------------
18
19 Implementation of an InstanceEditor demo plugin for the Traits UI demo program.
20
21 This demo shows each of the four styles of the InstanceEditor
22
23 Fixme: This version of the demo only shows the old-style InstanceEditor
24 capabilities.
25
26 Please refer to the `InstanceEditor API docs`_ for further information.
27
28 .. _InstanceEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.instance_editor.html#traitsui.editors.instance_editor.InstanceEditor
29 """
30 # Issue related to the demo warning: enthought/traitsui#939
31
32
33 from traits.api import HasTraits, Str, Range, Bool, Instance
34
35 from traitsui.api import Item, Group, View
36
37
38 # -------------------------------------------------------------------------
39 # Classes:
40 # -------------------------------------------------------------------------
41
42
43 class SampleClass(HasTraits):
44 """This Sample class is used to demonstrate the InstanceEditor demo."""
45
46 # The actual attributes don't matter here; we just need an assortment
47 # to demonstrate the InstanceEditor's capabilities.:
48 name = Str()
49 occupation = Str()
50 age = Range(21, 65)
51 registered_voter = Bool()
52
53 # The InstanceEditor uses whatever view is defined for the class. The
54 # default view lists the fields alphabetically, so it's best to define one
55 # explicitly:
56 traits_view = View('name', 'occupation', 'age', 'registered_voter')
57
58
59 class InstanceEditorDemo(HasTraits):
60 """This class specifies the details of the InstanceEditor demo."""
61
62 # Create an Instance trait to view:
63 sample_instance = Instance(SampleClass, ())
64
65 # Items are used to define the demo display, one item per editor style:
66 inst_group = Group(
67 Item('sample_instance', style='simple', label='Simple', id="simple"),
68 Item('_'),
69 Item('sample_instance', style='custom', label='Custom', id="custom"),
70 Item('_'),
71 Item('sample_instance', style='text', label='Text'),
72 Item('_'),
73 Item('sample_instance', style='readonly', label='ReadOnly'),
74 )
75
76 # Demo View:
77 traits_view = View(
78 inst_group, title='InstanceEditor', buttons=['OK'], resizable=True
79 )
80
81
82 # Create the demo:
83 demo = InstanceEditorDemo()
84
85 # Run the demo (if invoked from the command line):
86 if __name__ == '__main__':
87 demo.configure_traits()