Debian Codegraph @

traitsui/examples/demo/Dynamic_Forms/dynamic_selector.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 changing a selection list, using Handler
13
14 One way to dynamically change the list of values shown by an EnumEditor.
15
16 This example demonstrates several useful Traits UI concepts. It dynamically
17 changes the values which an EnumEditor presents to the user for selection. It
18 does this with a custom *Handler* which is assigned to the view, listens for
19 changes in a viewed trait, and changes the selection list accordingly.
20
21 Various implementations of dynamic data retrieval are possible. This example
22 shows how a Handler can interact with the traits in a view, separating model
23 logic from the view implementation.
24
25 Demo class *Address* has a simple set of attributes: *street_address*, *state*
26 and *city*. The values of *state* and *city* are to be chosen from enumerated
27 lists; however, the user does not want to see every city in the USA, but only
28 those for the chosen state.
29
30 Note that *city* is simply defined as a trait of type Str. By default, a Str
31 would be displayed using a simple TextEditor, but in this view we explicitly
32 specify that *city* should be displayed with an EnumEditor. The values that
33 appear in the GUI's enumerated list are determined by the *cities* attribute of
34 the view's handler, as specified in the EnumEditor's *name* parameter.
35 """
36
37 from traits.api import HasTraits, Str, Enum, List
38 from traitsui.api import View, Item, Handler, EnumEditor
39
40 # Dictionary of defined states and cities.
41 cities = {
42 'GA': ['Athens', 'Atlanta', 'Macon', 'Marietta', 'Savannah'],
43 'TX': ['Austin', 'Amarillo', 'Dallas', 'Houston', 'San Antonio', 'Waco'],
44 'OR': ['Albany', 'Eugene', 'Portland'],
45 }
46
47
48 class AddressHandler(Handler):
49 """
50 Handler class to redefine the possible values of 'city' based on 'state'.
51 This handler will be assigned to a view of an Address, and can listen and
52 respond to changes in the viewed Address.
53 """
54
55 # Current list of available cities:
56 cities = List(Str)
57
58 def object_state_changed(self, info):
59 """
60 This method listens for a change in the *state* attribute of the
61 object (Address) being viewed.
62
63 When this listener method is called, *info.object* is a reference to
64 the viewed object (Address).
65
66 """
67 # Change the list of available cities
68 self.cities = cities[info.object.state]
69
70 # As default value, use the first city in the list:
71 info.object.city = self.cities[0]
72
73
74 class Address(HasTraits):
75 """Demo class for demonstrating dynamic redefinition of valid trait values."""
76
77 street_address = Str()
78 state = Enum(list(cities.keys())[0], list(cities.keys()))
79 city = Str()
80
81 view = View(
82 Item(name='street_address'),
83 Item(name='state'),
84 Item(
85 name='city',
86 editor=EnumEditor(name='handler.cities'),
87 ),
88 title='Address Information',
89 buttons=['OK'],
90 resizable=True,
91 handler=AddressHandler,
92 )
93
94
95 # Create the demo:
96 demo = Address(street_address="4743 Dudley Lane")
97
98 # Run the demo (if invoked from the command line):
99 if __name__ == '__main__':
100 demo.configure_traits()