| 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 |
Displaying a list of objects in notebook tabs |
| 13 |
|
| 14 |
A list of objects can be displayed in a tabbed notebook, one object per tab. |
| 15 |
|
| 16 |
This example also shows how the currently active notebook tab of a ListEditor |
| 17 |
can be controlled using the ListEditor's 'selected' trait. |
| 18 |
|
| 19 |
Note the interaction between the spinner control (for the 'index' trait) and |
| 20 |
the currently selected notebook tab. Try changing the spinner value, then try |
| 21 |
clicking on various notebook tabs. |
| 22 |
|
| 23 |
Finally, note that the ListEditor will automatically scroll the tabs to make |
| 24 |
the selected tab completely visible. |
| 25 |
""" |
| 26 |
|
| 27 |
# The following text was removed from the module docstring (only works in wx): |
| 28 |
# Also note that rearranging the notebook tabs (using drag and drop) does not |
| 29 |
# affect the correspondence between the index value and its associated notebook |
| 30 |
# tab. The correspondence is determined by the contents of the 'people' trait, |
| 31 |
# and not by the physical layout of the notebook tabs. |
| 32 |
|
| 33 |
|
| 34 |
from traits.api import HasStrictTraits, Str, Int, Regex, List, Instance, Range |
| 35 |
from traitsui.api import View, VGroup, Item, ListEditor |
| 36 |
|
| 37 |
# -- Person Class --------------------------------------------------------- |
| 38 |
|
| 39 |
|
| 40 |
class Person(HasStrictTraits): |
| 41 |
|
| 42 |
# Trait definitions: |
| 43 |
name = Str() |
| 44 |
age = Int() |
| 45 |
phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') |
| 46 |
|
| 47 |
# Traits view definition: |
| 48 |
traits_view = View( |
| 49 |
'name', 'age', 'phone', width=0.18, buttons=['OK', 'Cancel'] |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
# -- Sample Data ---------------------------------------------------------- |
| 54 |
|
| 55 |
people = [ |
| 56 |
Person(name='Dave Chomsky', age=39, phone='555-1212'), |
| 57 |
Person(name='Mike Wakowski', age=28, phone='555-3526'), |
| 58 |
Person(name='Joe Higginbotham', age=34, phone='555-6943'), |
| 59 |
Person(name='Tom Derringer', age=22, phone='555-7586'), |
| 60 |
Person(name='Dick Van Der Hooten', age=63, phone='555-3895'), |
| 61 |
Person(name='Harry McCallum', age=46, phone='555-3285'), |
| 62 |
Person(name='Sally Johnson', age=43, phone='555-8797'), |
| 63 |
Person(name='Fields Timberlawn', age=31, phone='555-3547'), |
| 64 |
] |
| 65 |
|
| 66 |
# -- ListEditorNotebookSelectionDemo Class -------------------------------- |
| 67 |
|
| 68 |
|
| 69 |
class ListEditorNotebookSelectionDemo(HasStrictTraits): |
| 70 |
|
| 71 |
# -- Trait Definitions ---------------------------------------------------- |
| 72 |
|
| 73 |
# List of people: |
| 74 |
people = List(Person) |
| 75 |
|
| 76 |
# The currently selected person: |
| 77 |
selected = Instance(Person) |
| 78 |
|
| 79 |
# The index of the currently selected person: |
| 80 |
index = Range(0, 7, mode='spinner') |
| 81 |
|
| 82 |
# -- Traits View Definitions ---------------------------------------------- |
| 83 |
|
| 84 |
traits_view = View( |
| 85 |
Item('index'), |
| 86 |
'_', |
| 87 |
VGroup( |
| 88 |
Item( |
| 89 |
'people', |
| 90 |
id='notebook', |
| 91 |
show_label=False, |
| 92 |
style='custom', |
| 93 |
editor=ListEditor( |
| 94 |
use_notebook=True, |
| 95 |
deletable=False, |
| 96 |
selected='selected', |
| 97 |
export='DockWindowShell', |
| 98 |
page_name='.name', |
| 99 |
), |
| 100 |
) |
| 101 |
), |
| 102 |
id='traitsui.demo.Traits UI Demo.Advanced.' |
| 103 |
'List_editor_notebook_selection_demo', |
| 104 |
dock='horizontal', |
| 105 |
) |
| 106 |
|
| 107 |
# -- Trait Event Handlers ------------------------------------------------- |
| 108 |
|
| 109 |
def _selected_changed(self, selected): |
| 110 |
self.index = self.people.index(selected) |
| 111 |
|
| 112 |
def _index_changed(self, index): |
| 113 |
self.selected = self.people[index] |
| 114 |
|
| 115 |
|
| 116 |
# -- Set Up The Demo ------------------------------------------------------ |
| 117 |
|
| 118 |
demo = ListEditorNotebookSelectionDemo(people=people) |
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
demo.configure_traits() |