Debian Codegraph @

traitsui/examples/demo/Advanced/Multi_select_string_list.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 Creating a multi-select list box
20
21 How to use a TabularEditor to create a multi-select list box.
22
23 This demo uses two TabularEditors, side-by-side. Selections from the left table
24 are shown in the right table. Each table has only one column.
25
26 """
27 # Issues related to the demo warning: enthought/traitsui#14,
28 # enthought/traitsui#15, enthought/traitsui#960
29
30 from traits.api import HasPrivateTraits, List, Str, Property
31 from traitsui.api import View, HGroup, UItem, TabularAdapter, TabularEditor
32
33
34 class MultiSelectAdapter(TabularAdapter):
35 """This adapter is used by both the left and right tables"""
36
37 # Titles and column names for each column of a table.
38 # In this example, each table has only one column.
39 columns = [('', 'myvalue')]
40
41 # Magically named trait which gives the display text of the column named
42 # 'myvalue'. This is done using a Traits Property and its getter:
43 myvalue_text = Property()
44
45 # The getter for Property 'myvalue_text' simply takes the value of the
46 # corresponding item in the list being displayed in this table.
47 # A more complicated example could format the item before displaying it.
48 def _get_myvalue_text(self):
49 return self.item
50
51
52 class MultiSelect(HasPrivateTraits):
53 """This is the class used to view two tables"""
54
55 # FIXME (TraitsUI defect #14): When multi-select is done by keyboard
56 # (shift+arrow), the 'selected' trait list does not update.
57
58 # FIXME (TraitsUI defect #15): In Windows wx, when show_titles is False,
59 # left table does not draw until selection passes through all rows.
60 # (Workaround here: set show_titles True and make column titles empty.)
61
62 choices = List(Str)
63 selected = List(Str)
64
65 traits_view = View(
66 HGroup(
67 UItem(
68 'choices',
69 editor=TabularEditor(
70 show_titles=True,
71 selected='selected',
72 editable=False,
73 multi_select=True,
74 adapter=MultiSelectAdapter(),
75 ),
76 ),
77 UItem(
78 'selected',
79 editor=TabularEditor(
80 show_titles=True,
81 editable=False,
82 adapter=MultiSelectAdapter(),
83 ),
84 ),
85 ),
86 resizable=True,
87 width=200,
88 height=300,
89 )
90
91
92 # Create the demo:
93 demo = MultiSelect(
94 choices=[
95 'one',
96 'two',
97 'three',
98 'four',
99 'five',
100 'six',
101 'seven',
102 'eight',
103 'nine',
104 'ten',
105 ]
106 )
107
108 # Run the demo (if invoked from the command line):
109 if __name__ == '__main__':
110 demo.configure_traits()