Debian Codegraph @

traitsui/examples/demo/Dynamic_Forms/visible_when.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 Dynamic restructuring a user interface using 'visible_when'
13
14 How to dynamically change which components of a Traits UI view are visible,
15 depending on the value of another trait.
16
17 The demo class "Person" has a set of attributes that apply to all instances
18 ('first_name', 'last_name', 'age'), a set of attributes that apply only to
19 children (Persons whose age is under 18), and a set of attributes that apply
20 only to adults. As a Person's age changes, only the age-appropriate attributes
21 will be visible.
22
23 **Detail:** The optional *visible_when* attribute of an Item or Group is a
24 string containing a boolean expression (logical condition) indicating when this
25 Item or Group will be visible. The boolean expression is evaluated for the
26 object being viewed, so that in this example, 'age' refers to the 'age'
27 attribute of the Person being viewed.
28
29 Compare this to very similar demo of *enabled_when*, and the visually similar,
30 but structurally very different demo of *dynamic restructuring of a user
31 interface using an Instance editor and a Handler*.
32 """
33
34 from traits.api import HasTraits, Str, Range, Bool, Enum
35 from traitsui.api import Item, Group, View, Label
36
37
38 class Person(HasTraits):
39 """Example of restructuring a user interface by controlling visibility."""
40
41 # General traits:
42 first_name = Str()
43 last_name = Str()
44 age = Range(0, 120)
45
46 # Traits for children only:
47 legal_guardian = Str()
48 school = Str()
49 grade = Range(1, 12)
50
51 # Traits for adults only:
52 marital_status = Enum('single', 'married', 'divorced', 'widowed')
53 registered_voter = Bool(False)
54 military_service = Bool(False)
55
56 # Interface for attributes that are always visible in interface:
57 gen_group = Group(
58 Item(name='first_name'),
59 Item(name='last_name'),
60 Item(name='age'),
61 label='General Info',
62 show_border=True,
63 )
64
65 # Interface for attributes of Persons under 18:
66 child_group = Group(
67 Item(name='legal_guardian'),
68 Item(name='school'),
69 Item(name='grade'),
70 label='Additional Info for minors',
71 show_border=True,
72 visible_when='age < 18',
73 )
74
75 # Interface for attributes of Persons 18 and over:
76 adult_group = Group(
77 Item(name='marital_status'),
78 Item(name='registered_voter'),
79 Item(name='military_service'),
80 label='Additional Info for adults',
81 show_border=True,
82 visible_when='age >= 18',
83 )
84
85 # A simple View is sufficient, since the Group definitions do all the work:
86 view = View(
87 Group(
88 gen_group,
89 '10',
90 Label("Using 'visible_when':"),
91 '10',
92 child_group,
93 adult_group,
94 ),
95 title='Personal Information',
96 resizable=True,
97 buttons=['OK'],
98 )
99
100
101 # Create the demo:
102 demo = Person(first_name="Samuel", last_name="Johnson", age=16)
103
104 # Run the demo (if invoked from the command line):
105 if __name__ == '__main__':
106 demo.configure_traits()