diff --git a/docs/source/traitsui_user_manual/adapters.rst b/docs/source/traitsui_user_manual/adapters.rst index 4d4c66adb..f96d5419f 100644 --- a/docs/source/traitsui_user_manual/adapters.rst +++ b/docs/source/traitsui_user_manual/adapters.rst @@ -208,8 +208,10 @@ such a structure might look like this:: return bool(self.value) def tno_get_children(self, node): - return [DictNode(parent=self, label=key, value=value) - for key, value in sorted(self.value.items())] + return [ + DictNode(parent=self, label=key, value=value) + for key, value in sorted(self.value.items()) + ] and so forth. There is additional work if you want to be able to modify the structure of the tree, for example. In addition to defining the @@ -360,9 +362,11 @@ Say instead that you have a list of :py:class:`Person` objects, with display in the table. Then you could use the following :py:attr:`~TabularAdapter.columns` value instead:: - columns = [('Name', 'name'), - ('Age', 'age'), - ('Weight', 'weight')] + columns = [ + ('Name', 'name'), + ('Age', 'age'), + ('Weight', 'weight'), + ] In this case, the *column ids* are the names of the traits you want to display in each column. diff --git a/docs/source/traitsui_user_manual/advanced_view.rst b/docs/source/traitsui_user_manual/advanced_view.rst index 931a8c709..05607fffb 100644 --- a/docs/source/traitsui_user_manual/advanced_view.rst +++ b/docs/source/traitsui_user_manual/advanced_view.rst @@ -63,30 +63,8 @@ variation on Example 3: .. rubric:: Example 5: Using configure_traits() with a default View object -:: - - # default_traits_view.py -- Sample code to demonstrate the use of - # 'traits_view' - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item, Group - import traitsui - - class SimpleEmployee2(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - traits_view = View(Group(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - label = 'Personnel profile', - show_border = True)) - - sam = SimpleEmployee2() - sam.configure_traits() +.. literalinclude:: examples/default_traits_view.py + :start-at: default_traits_view.py In this example, configure_traits() no longer requires a *view* keyword argument; the **traits_view** attribute is used by default, resulting in the @@ -115,31 +93,8 @@ example above would be implemented as follows: .. rubric:: Example 5b: Building a default View object with default_traits_view() -:: - - # default_traits_view2.py -- Sample code to demonstrate the use of - # 'default_traits_view' - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item, Group - import traitsui - - class SimpleEmployee2(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - def default_traits_view(self): - return View(Group(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - label = 'Personnel profile', - show_border = True)) - - sam = SimpleEmployee2() - sam.configure_traits() +.. literalinclude:: examples/default_traits_view2.py + :start-at: default_traits_view2.py This pattern can be useful for situations where the layout of GUI elements depends on the state of the object. For instance, to populate the values of a @@ -165,40 +120,8 @@ this by simply adding a second View attribute: .. rubric:: Example 6: Defining multiple View objects in a HasTraits class -:: - - # multiple_views.py -- Sample code to demonstrate the use of - # multiple views - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item, Group - import traitsui - - class SimpleEmployee3(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - traits_view = View(Group(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - label = 'Personnel profile', - show_border = True)) - - all_view = View(Group(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - Item(name = 'employee_number'), - Item(name = 'salary'), - label = 'Personnel database ' + - 'entry', - show_border = True)) - - sam = SimpleEmployee3() - sam.configure_traits() - sam.configure_traits(view='all_view') +.. literalinclude:: examples/multiple_views.py + :start-at: multiple_views.py .. index:: traits_view attribute, configure_traits(); view parameter @@ -240,9 +163,15 @@ UI, you can define a named View wherever you can define a variable or class attribute. [7]_ A View can even be defined in-line as a function or method argument, for example:: - object.configure_traits(view=View(Group(Item(name='a'), - Item(name='b'), - Item(name='c'))) + object.configure_traits( + view=View( + Group( + Item(name='a'), + Item(name='b'), + Item(name='c'), + ), + ), + ) However, this approach is apt to obfuscate the code unless the View is very simple. @@ -387,55 +316,8 @@ example shows: .. rubric:: Example 7: Using a multi-object view with a context -:: - - # multi_object_view.py -- Sample code to show multi-object view - # with context - - from traits.api import HasTraits, Str, Int, Bool - from traitsui.api import View, Group, Item - - # Sample class - class House(HasTraits): - address = Str() - bedrooms = Int() - pool = Bool() - price = Int() - - # View object designed to display two objects of class 'House' - comp_view = View( - Group( - Group( - Item('h1.address', resizable=True), - Item('h1.bedrooms'), - Item('h1.pool'), - Item('h1.price'), - show_border=True - ), - Group( - Item('h2.address', resizable=True), - Item('h2.bedrooms'), - Item('h2.pool'), - Item('h2.price'), - show_border=True - ), - orientation = 'horizontal' - ), - title = 'House Comparison' - ) - # A pair of houses to demonstrate the View - house1 = House(address='4743 Dudley Lane', - bedrooms=3, - pool=False, - price=150000) - house2 = House(address='11604 Autumn Ridge', - bedrooms=3, - pool=True, - price=200000) - - # ...And the actual display command - house1.configure_traits(view=comp_view, context={'h1':house1, - 'h2':house2}) +.. literalinclude:: examples/multi_object_view.py + :start-at: multi_object_view.py .. FIXME: This is a bit assymmetrical. Can we clean it up without complicating the example overly? @@ -505,21 +387,21 @@ the following two definitions, taken together, are equivalent to the third: :: # This fragment... - my_view = View(Group(Item('a'), - Item('b')), - Include('my_group')) + my_view = View( + Group(Item('a'), Item('b')), + Include('my_group'), + ) # ...plus this fragment... - my_group = Group(Item('c'), - Item('d'), - Item('e')) + my_group = Group( + Item('c'), Item('d'), Item('e'), + ) #...are equivalent to this: - my_view = View(Group(Item('a'), - Item('b')), - Group(Item('c'), - Item('d'), - Item('e')) + my_view = View( + Group(Item('a'), Item('b')), + Group(Item('c'), Item('d'), Item('e')), + ) This opens an interesting possibility when a View is part of a model class: any Include objects belonging to that View can be defined differently for different diff --git a/docs/source/traitsui_user_manual/custom_view.rst b/docs/source/traitsui_user_manual/custom_view.rst index 276e3889a..0da9c4139 100644 --- a/docs/source/traitsui_user_manual/custom_view.rst +++ b/docs/source/traitsui_user_manual/custom_view.rst @@ -101,35 +101,8 @@ pages that a user must navigate sequentially. .. rubric:: Example 3.1: Displaying a view the "wizard" style -:: - - # wizard.py ---Example of a traits-based wizard UI - - from traits.api import HasTraits, Str - from traitsui.api import Item, View, VGroup - - class Person(HasTraits): - first_name = Str() - last_name = Str() - - company = Str() - position = Str() - - view = View( - VGroup( - Item("first_name"), - Item("last_name") - ), - VGroup( - Item("company"), - Item("position") - ) - ) - - person = Person(first_name='Postman', last_name='Pat', company="Enthought", - position="Software Developer") - person.configure_traits(kind='wizard') - +.. literalinclude:: examples/wizard.py + :start-at: wizard.py leads to the following 2 modal dialogs: @@ -194,30 +167,8 @@ Consider the following variation on Example 3: .. rubric:: Example 4: Using a View object with buttons -:: - - # configure_traits_view_buttons.py -- Sample code to demonstrate - # configure_traits() - - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item - from traitsui.menu import OKButton, CancelButton - - class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - view1 = View(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - buttons = [OKButton, CancelButton]) - - sam = SimpleEmployee() - sam.configure_traits(view=view1) +.. literalinclude:: examples/configure_traits_view_buttons.py + :start-at: configure_traits_view_buttons.py The resulting window has the same content as before, but now two buttons are displayed at the bottom: :guilabel:`OK` and :guilabel:`Cancel`: @@ -257,16 +208,18 @@ from traitsui.menu and assigned to the buttons attribute: .. index:: OKCancelsButtons, ModalButtons, LiveButtons -* OKCancelButtons = ``[OKButton, CancelButton ]`` -* ModalButtons = ``[ ApplyButton, RevertButton, OKButton, CancelButton, HelpButton ]`` -* LiveButtons = ``[ UndoButton, RevertButton, OKButton, CancelButton, HelpButton ]`` +* OKCancelButtons = ``[OKButton, CancelButton]`` +* ModalButtons = ``[ApplyButton, RevertButton, OKButton, CancelButton, HelpButton]`` +* LiveButtons = ``[UndoButton, RevertButton, OKButton, CancelButton, HelpButton]`` Thus, one could rewrite the lines in Example 4 as follows, and the effect would be exactly the same:: from traitsui.menu import OKCancelButtons - buttons = OKCancelButtons + ... + buttons=OKCancelButtons, + ) .. index:: NoButtons diff --git a/docs/source/traitsui_user_manual/examples/array_editor.py b/docs/source/traitsui_user_manual/examples/array_editor.py new file mode 100644 index 000000000..0431f0888 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/array_editor.py @@ -0,0 +1,42 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# array_editor.py -- Example of using array editors + +import numpy as np + +from traits.api import Array, HasPrivateTraits +from traitsui.api import ArrayEditor, Item, View +from traitsui.menu import NoButtons + + +class ArrayEditorTest(HasPrivateTraits): + + three = Array(np.int, (3, 3)) + + four = Array( + np.float, (4, 4), editor=ArrayEditor(width=-50), + ) + + view = View( + Item('three', label='3x3 Integer'), + '_', + Item('three', label='Integer Read-only', style='readonly'), + '_', + Item('four', label='4x4 Float'), + '_', + Item('four', label='Float Read-only', style='readonly'), + buttons=NoButtons, + resizable=True, + ) + + +if __name__ == '__main__': + ArrayEditorTest().configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/configure_traits_view.py b/docs/source/traitsui_user_manual/examples/configure_traits_view.py new file mode 100644 index 000000000..c8bfae70d --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/configure_traits_view.py @@ -0,0 +1,32 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# configure_traits_view.py -- Sample code to demonstrate configure_traits() + +from traits.api import HasTraits, Int, Str +from traitsui.api import Item, View + + +class SimpleEmployee(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + employee_number = Str() + salary = Int() + + +view1 = View( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), +) + +sam = SimpleEmployee() +sam.configure_traits(view=view1) diff --git a/docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py b/docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py new file mode 100644 index 000000000..7cc227578 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py @@ -0,0 +1,35 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# configure_traits_view_buttons.py -- Sample code to demonstrate +# configure_traits() + +from traits.api import HasTraits, Str, Int +from traitsui.api import CancelButton, Item, OKButton, View + + +class SimpleEmployee(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + + employee_number = Str() + salary = Int() + + +view1 = View( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + buttons=[OKButton, CancelButton], +) + +sam = SimpleEmployee() +sam.configure_traits(view=view1) diff --git a/docs/source/traitsui_user_manual/examples/configure_traits_view_group.py b/docs/source/traitsui_user_manual/examples/configure_traits_view_group.py new file mode 100644 index 000000000..9a1a7494d --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/configure_traits_view_group.py @@ -0,0 +1,38 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# configure_traits_view_group.py -- Sample code to demonstrate +# configure_traits() + +from traits.api import HasTraits, Int, Str +from traitsui.api import Group, Item, View + + +class SimpleEmployee(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + + employee_number = Str() + salary = Int() + + +view1 = View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + label='Personnel profile', + show_border=True, + ), +) + +sam = SimpleEmployee() +sam.configure_traits(view=view1) diff --git a/docs/source/traitsui_user_manual/examples/default_trait_editors.py b/docs/source/traitsui_user_manual/examples/default_trait_editors.py new file mode 100644 index 000000000..9f3ebe17b --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/default_trait_editors.py @@ -0,0 +1,38 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# default_trait_editors.py -- Example of using default trait editors + +from traits.api import Bool, HasTraits, Range, Str +from traitsui.api import Item, View + + +class Adult(HasTraits): + first_name = Str() + last_name = Str() + age = Range(21, 99) + registered_voter = Bool() + + traits_view = View( + Item(name='first_name'), + Item(name='last_name'), + Item(name='age'), + Item(name='registered_voter'), + ) + + +alice = Adult( + first_name='Alice', + last_name='Smith', + age=42, + registered_voter=True, +) + +alice.configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/default_traits_view.py b/docs/source/traitsui_user_manual/examples/default_traits_view.py new file mode 100644 index 000000000..c2cef102b --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/default_traits_view.py @@ -0,0 +1,37 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# default_traits_view.py -- Sample code to demonstrate the use of 'traits_view' + +from traits.api import HasTraits, Int, Str +from traitsui.api import Group, Item, View + + +class SimpleEmployee2(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + + employee_number = Str() + salary = Int() + + traits_view = View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + label='Personnel profile', + show_border=True, + ), + ) + + +sam = SimpleEmployee2() +sam.configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/default_traits_view2.py b/docs/source/traitsui_user_manual/examples/default_traits_view2.py new file mode 100644 index 000000000..bb02b399c --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/default_traits_view2.py @@ -0,0 +1,39 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# default_traits_view2.py -- Sample code to demonstrate the use of +# 'default_traits_view' + +from traits.api import HasTraits, Str, Int +from traitsui.api import View, Item, Group + + +class SimpleEmployee2(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + + employee_number = Str() + salary = Int() + + def default_traits_view(self): + return View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + label='Personnel profile', + show_border=True, + ), + ) + + +sam = SimpleEmployee2() +sam.configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/enum_editor.py b/docs/source/traitsui_user_manual/examples/enum_editor.py new file mode 100644 index 000000000..ddd14b9ce --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/enum_editor.py @@ -0,0 +1,36 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# enum_editor.py -- Example of using an enumeration editor + +from traits.api import Enum, HasTraits +from traitsui.api import EnumEditor, Item, View + + +class EnumExample(HasTraits): + priority = Enum('Medium', 'Highest', 'High', 'Low', 'Lowest') + + view = View( + Item( + name='priority', + editor=EnumEditor( + values={ + 'Highest': '1:Highest', + 'High': '2:High', + 'Medium': '3:Medium', + 'Low': '4:Low', + 'Lowest': '5:Lowest', + } + ), + ), + ) + + +EnumExample().configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/handler_override.py b/docs/source/traitsui_user_manual/examples/handler_override.py new file mode 100644 index 000000000..000782d0c --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/handler_override.py @@ -0,0 +1,46 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# handler_override.py -- Example of a Handler that overrides setattr(), and +# that has a user interface notification method + +from traits.api import Bool, HasTraits +from traitsui.api import Handler, View + + +class TC_Handler(Handler): + + def setattr(self, info, object, name, value): + Handler.setattr(self, info, object, name, value) + info.object._updated = True + + def object__updated_changed(self, info): + if info.initialized: + info.ui.title += "*" + + +class TestClass(HasTraits): + b1 = Bool() + b2 = Bool() + b3 = Bool() + _updated = Bool(False) + + +view1 = View( + 'b1', + 'b2', + 'b3', + title="Alter Title", + handler=TC_Handler(), + buttons=['OK', 'Cancel'], +) + +tc = TestClass() +tc.configure_traits(view=view1) diff --git a/docs/source/traitsui_user_manual/examples/instance_editor_selection.py b/docs/source/traitsui_user_manual/examples/instance_editor_selection.py new file mode 100644 index 000000000..4ffb3458c --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/instance_editor_selection.py @@ -0,0 +1,59 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# instance_editor_selection.py -- Example of an instance editor with instance +# selection + +from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str +from traitsui.api import InstanceEditor, Item, View + + +class Person(HasStrictTraits): + name = Str() + age = Int() + phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') + + traits_view = View('name', 'age', 'phone') + + +people = [ + Person(name='Dave', age=39, phone='555-1212'), + Person(name='Mike', age=28, phone='555-3526'), + Person(name='Joe', age=34, phone='555-6943'), + Person(name='Tom', age=22, phone='555-7586'), + Person(name='Dick', age=63, phone='555-3895'), + Person(name='Harry', age=46, phone='555-3285'), + Person(name='Sally', age=43, phone='555-8797'), + Person(name='Fields', age=31, phone='555-3547'), +] + + +class Team(HasStrictTraits): + + name = Str() + captain = Instance(Person) + roster = List(Person) + + traits_view = View( + Item('name'), + Item('_'), + Item( + 'captain', + label='Team Captain', + editor=InstanceEditor(name='roster', editable=True), + style='custom', + ), + buttons=['OK'], + ) + + +if __name__ == '__main__': + team = Team(name='Vultures', captain=people[0], roster=people) + team.configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/key_bindings.py b/docs/source/traitsui_user_manual/examples/key_bindings.py new file mode 100644 index 000000000..e2c72271f --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/key_bindings.py @@ -0,0 +1,76 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# key_bindings.py -- Example of a code editor with a key bindings editor + +from traits.api import Button, Code, HasPrivateTraits, Str +from traitsui.api import Group, Handler, Item, View +from traitsui.key_bindings import KeyBinding, KeyBindings + +key_bindings = KeyBindings( + KeyBinding( + binding1='Ctrl-s', + description='Save to a file', + method_name='save_file', + ), + KeyBinding( + binding1='Ctrl-r', + description='Run script', + method_name='run_script', + ), + KeyBinding( + binding1='Ctrl-k', + description='Edit key bindings', + method_name='edit_bindings', + ), +) + + +class CodeHandler(Handler): + """ Handler class for bound methods. """ + + def save_file(self, info): + info.object.status = "save file" + + def run_script(self, info): + info.object.status = "run script" + + def edit_bindings(self, info): + info.object.status = "edit bindings" + key_bindings.edit_traits() + + +class KBCodeExample(HasPrivateTraits): + + code = Code() + status = Str() + kb = Button(label='Edit Key Bindings') + + view = View( + Group( + Item('code', style='custom', resizable=True), + Item('status', style='readonly'), + 'kb', + orientation='vertical', + show_labels=False, + ), + id='KBCodeExample', + key_bindings=key_bindings, + title='Code Editor With Key Bindings', + resizable=True, + handler=CodeHandler(), + ) + + def _kb_fired(self, event): + key_bindings.edit_traits() + + +if __name__ == '__main__': + KBCodeExample().configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/mixed_styles.py b/docs/source/traitsui_user_manual/examples/mixed_styles.py new file mode 100644 index 000000000..08a5a0e95 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/mixed_styles.py @@ -0,0 +1,40 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# mixed_styles.py -- Example of using editor styles at various levels + +from traits.api import Enum, HasTraits, Str +from traitsui.api import Group, Item, View + + +class MixedStyles(HasTraits): + first_name = Str() + last_name = Str() + + department = Enum("Business", "Research", "Admin") + position_type = Enum("Full-Time", "Part-Time", "Contract") + + traits_view = View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Group( + Item(name='department'), + Item(name='position_type', style='custom'), + style='simple', + ), + ), + title='Mixed Styles', + style='readonly', + ) + + +ms = MixedStyles(first_name='Sam', last_name='Smith') +ms.configure_traits() diff --git a/docs/source/traitsui_user_manual/examples/multi_object_view.py b/docs/source/traitsui_user_manual/examples/multi_object_view.py new file mode 100644 index 000000000..60a8430c0 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/multi_object_view.py @@ -0,0 +1,61 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# multi_object_view.py -- Sample code to show multi-object view with context + +from traits.api import Bool, HasTraits, Int, Str +from traitsui.api import Group, Item, View + + +class House(HasTraits): + address = Str() + bedrooms = Int() + pool = Bool() + price = Int() + + +# View object designed to display two objects of class 'House' +comp_view = View( + Group( + Group( + Item('h1.address', resizable=True), + Item('h1.bedrooms'), + Item('h1.pool'), + Item('h1.price'), + show_border=True, + ), + Group( + Item('h2.address', resizable=True), + Item('h2.bedrooms'), + Item('h2.pool'), + Item('h2.price'), + show_border=True, + ), + orientation='horizontal', + ), + title='House Comparison', +) + +# A pair of houses to demonstrate the View +house1 = House( + address='4743 Dudley Lane', + bedrooms=3, + pool=False, + price=150000, +) +house2 = House( + address='11604 Autumn Ridge', + bedrooms=3, + pool=True, + price=200000, +) + +# ...And the actual display command +house1.configure_traits(view=comp_view, context={'h1': house1, 'h2': house2}) diff --git a/docs/source/traitsui_user_manual/examples/multiple_views.py b/docs/source/traitsui_user_manual/examples/multiple_views.py new file mode 100644 index 000000000..77815a0b7 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/multiple_views.py @@ -0,0 +1,50 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# multiple_views.py -- Sample code to demonstrate the use of multiple views + +from traits.api import HasTraits, Str, Int +from traitsui.api import View, Item, Group + + +class SimpleEmployee3(HasTraits): + first_name = Str() + last_name = Str() + department = Str() + + employee_number = Str() + salary = Int() + + traits_view = View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + label='Personnel profile', + show_border=True, + ), + ) + + all_view = View( + Group( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + Item(name='employee_number'), + Item(name='salary'), + label='Personnel database entry', + show_border=True, + ), + ) + + +sam = SimpleEmployee3() +sam.configure_traits() +sam.configure_traits(view='all_view') diff --git a/docs/source/traitsui_user_manual/examples/tree_editor.py b/docs/source/traitsui_user_manual/examples/tree_editor.py new file mode 100644 index 000000000..603591a55 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/tree_editor.py @@ -0,0 +1,184 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# tree_editor.py -- Example of a tree editor + +from traits.api import HasTraits, Instance, List, Str, Regex +from traitsui.api import ( + Action, Group, Handler, HGroup, Item, Menu, Separator, TreeEditor, + TreeNode, View, VSplit, +) +from traitsui.editors.tree_editor import ( + NewAction, CopyAction, CutAction, PasteAction, DeleteAction, RenameAction, +) + + +class Employee(HasTraits): + name = Str('') + title = Str() + phone = Regex(regex=r'\d\d\d-\d\d\d\d') + + def default_title(self): + self.title = 'Senior Engineer' + + +class Department(HasTraits): + name = Str('') + employees = List(Employee) + + +class Company(HasTraits): + name = Str('') + departments = List(Department) + employees = List(Employee) + + +class Owner(HasTraits): + name = Str('') + company = Instance(Company) + + +jason = Employee(name='Jason', title='Engineer', phone='536-1057') +mike = Employee(name='Mike', title='Sr. Marketing Analyst', phone='536-1057') +dave = Employee(name='Dave', title='Sr. Engineer', phone='536-1057') +susan = Employee(name='Susan', title='Engineer', phone='536-1057') +betty = Employee(name='Betty', title='Marketing Analyst') + +owner = Owner( + name='wile', + company=Company( + name='Acme Labs, Inc.', + departments=[ + Department(name='Marketing', employees=[mike, betty]), + Department(name='Engineering', employees=[dave, susan, jason]), + ], + employees=[dave, susan, mike, betty, jason], + ), +) + +# View for objects that aren't edited +no_view = View() + +# Actions used by tree editor context menu +def_title_action = Action(name='Default title', action='object.default') + +dept_action = Action( + name='Department', + action='handler.employee_department(editor,object)', +) + +# View used by tree editor +employee_view = View( + VSplit( + HGroup('3', 'name'), + HGroup('9', 'title'), + HGroup('phone'), + id='vsplit', + ), + id='traits.doc.example.treeeditor', + dock='vertical', +) + + +class TreeHandler(Handler): + + def employee_department(self, editor, object): + dept = editor.get_parent(object) + print(f'{object.name} works in the {dept.name} department.') + + +tree_editor = TreeEditor( + nodes=[ + TreeNode( + node_for=[Company], + auto_open=True, + children='', + label='name', + view=View(Group('name', orientation='vertical', show_left=True)), + ), + TreeNode( + node_for=[Company], + auto_open=True, + children='departments', + label='=Departments', + view=no_view, + add=[Department], + ), + TreeNode( + node_for=[Company], + auto_open=True, + children='employees', + label='=Employees', + view=no_view, + add=[Employee], + ), + TreeNode( + node_for=[Department], + auto_open=True, + children='employees', + label='name', + menu=Menu( + NewAction, + Separator(), + DeleteAction, + Separator(), + RenameAction, + Separator(), + CopyAction, + CutAction, + PasteAction, + ), + view=View(Group('name', orientation='vertical', show_left=True)), + add=[Employee], + ), + TreeNode( + node_for=[Employee], + auto_open=True, + label='name', + menu=Menu( + NewAction, + Separator(), + def_title_action, + dept_action, + Separator(), + CopyAction, + CutAction, + PasteAction, + Separator(), + DeleteAction, + Separator(), + RenameAction, + ), + view=employee_view, + ), + ], +) + +# The main view +view = View( + Group( + Item(name='company', id='company', editor=tree_editor, resizable=True), + orientation='vertical', + show_labels=True, + show_left=True, + ), + title='Company Structure', + id='traitsui.tests.tree_editor_test', + dock='horizontal', + drop_class=HasTraits, + handler=TreeHandler(), + buttons=['Undo', 'OK', 'Cancel'], + resizable=True, + width=.3, + height=.3, +) + +if __name__ == '__main__': + owner.configure_traits(view=view) diff --git a/docs/source/traitsui_user_manual/examples/wizard.py b/docs/source/traitsui_user_manual/examples/wizard.py new file mode 100644 index 000000000..3e7eea708 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/wizard.py @@ -0,0 +1,41 @@ +# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +# wizard.py -- Example of a traits-based wizard UI + +from traits.api import HasTraits, Str +from traits.etsconfig.api import ETSConfig +from traitsui.api import Item, View, VGroup + + +class Person(HasTraits): + first_name = Str() + last_name = Str() + + company = Str() + position = Str() + + view = View( + VGroup(Item("first_name"), Item("last_name")), + VGroup(Item("company"), Item("position")), + ) + + +person = Person( + first_name='Postman', + last_name='Pat', + company="Enthought", + position="Software Developer", +) + + +if ETSConfig.toolkit == "wx": + # Wizard window is currently only available for wx backend. + person.configure_traits(kind='wizard') diff --git a/docs/source/traitsui_user_manual/factories_advanced_extra.rst b/docs/source/traitsui_user_manual/factories_advanced_extra.rst index e0f7e3596..d22874be2 100644 --- a/docs/source/traitsui_user_manual/factories_advanced_extra.rst +++ b/docs/source/traitsui_user_manual/factories_advanced_extra.rst @@ -128,71 +128,8 @@ with associated key bindings, and a button that invokes the key binding editor. .. rubric:: Example 17: Code editor with key binding editor -:: - - # key_bindings.py -- Example of a code editor with a - # key bindings editor - - from traits.api \ - import Button, Code, HasPrivateTraits, Str - from traitsui.api \ - import View, Item, Group, Handler, CodeEditor - from traitsui.key_bindings \ - import KeyBinding, KeyBindings - - key_bindings = KeyBindings( - KeyBinding( binding1 = 'Ctrl-s', - description = 'Save to a file', - method_name = 'save_file' ), - KeyBinding( binding1 = 'Ctrl-r', - description = 'Run script', - method_name = 'run_script' ), - KeyBinding( binding1 = 'Ctrl-k', - description = 'Edit key bindings', - method_name = 'edit_bindings' ) - ) - - # TraitsUI Handler class for bound methods - class CodeHandler ( Handler ): - - def save_file ( self, info ): - info.object.status = "save file" - - def run_script ( self, info ): - info.object.status = "run script" - - def edit_bindings ( self, info ): - info.object.status = "edit bindings" - key_bindings.edit_traits() - - class KBCodeExample ( HasPrivateTraits ): - - code = Code() - status = Str() - kb = Button(label='Edit Key Bindings') - - view = View( Group ( - Item( 'code', - style = 'custom', - resizable = True ), - Item('status', style='readonly'), - 'kb', - orientation = 'vertical', - show_labels = False, - ), - id = 'KBCodeExample', - key_bindings = key_bindings, - title = 'Code Editor With Key Bindings', - resizable = True, - - handler = CodeHandler() ) - - def _kb_fired( self, event ): - key_bindings.edit_traits() - - - if __name__ == '__main__': - KBCodeExample().configure_traits() +.. literalinclude:: examples/key_bindings.py + :start-at: key_bindings.py .. _tableeditor: @@ -742,200 +679,8 @@ The following example shows the code that produces the editor shown in Figure .. rubric:: Example 18: Code for example tree editor -:: - - # tree_editor.py -- Example of a tree editor - - from traits.api \ - import HasTraits, Str, Regex, List, Instance - from traitsui.api \ - import TreeEditor, TreeNode, View, Item, VSplit, \ - HGroup, Handler, Group - from traitsui.menu \ - import Menu, Action, Separator - from traitsui.wx.tree_editor \ - import NewAction, CopyAction, CutAction, \ - PasteAction, DeleteAction, RenameAction - - # DATA CLASSES - - class Employee ( HasTraits ): - name = Str( '' ) - title = Str() - phone = Regex( regex = r'\d\d\d-\d\d\d\d' ) - - def default_title ( self ): - self.title = 'Senior Engineer' - - class Department ( HasTraits ): - name = Str( '' ) - employees = List( Employee ) - - - class Company ( HasTraits ): - name = Str( '' ) - departments = List( Department ) - employees = List( Employee ) - - class Owner ( HasTraits ): - name = Str( '' ) - company = Instance( Company ) - - # INSTANCES - - jason = Employee( - name = 'Jason', - title = 'Engineer', - phone = '536-1057' ) - - mike = Employee( - name = 'Mike', - title = 'Sr. Marketing Analyst', - phone = '536-1057' ) - - dave = Employee( - name = 'Dave', - title = 'Sr. Engineer', - phone = '536-1057' ) - - susan = Employee( - name = 'Susan', - title = 'Engineer', - phone = '536-1057' ) - - betty = Employee( - name = 'Betty', - title = 'Marketing Analyst' ) - - owner = Owner( - name = 'wile', - company = Company( - name = 'Acme Labs, Inc.', - departments = [ - Department( - name = 'Marketing', - employees = [ mike, betty ] - ), - Department( - name = 'Engineering', - employees = [ dave, susan, jason ] - ) - ], - employees = [ dave, susan, mike, betty, jason ] - ) - ) - - # View for objects that aren't edited - no_view = View() - - # Actions used by tree editor context menu - - def_title_action = Action(name='Default title', - action = 'object.default') - - dept_action = Action( - name='Department', - action='handler.employee_department(editor,object)') - - # View used by tree editor - employee_view = View( - VSplit( - HGroup( '3', 'name' ), - HGroup( '9', 'title' ), - HGroup( 'phone' ), - id = 'vsplit' ), - id = 'traits.doc.example.treeeditor', - dock = 'vertical' ) - - class TreeHandler ( Handler ): - - def employee_department ( self, editor, object ): - dept = editor.get_parent( object ) - print '%s works in the %s department.' %\ - ( object.name, dept.name ) - - # Tree editor - tree_editor = TreeEditor( - nodes = [ - TreeNode( node_for = [ Company ], - auto_open = True, - children = '', - label = 'name', - view = View( Group('name', - orientation='vertical', - show_left=True )) ), - TreeNode( node_for = [ Company ], - auto_open = True, - children = 'departments', - label = '=Departments', - view = no_view, - add = [ Department ] ), - TreeNode( node_for = [ Company ], - auto_open = True, - children = 'employees', - label = '=Employees', - view = no_view, - add = [ Employee ] ), - TreeNode( node_for = [ Department ], - auto_open = True, - children = 'employees', - label = 'name', - menu = Menu( NewAction, - Separator(), - DeleteAction, - Separator(), - RenameAction, - Separator(), - CopyAction, - CutAction, - PasteAction ), - view = View( Group ('name', - orientation='vertical', - show_left=True )), - add = [ Employee ] ), - TreeNode( node_for = [ Employee ], - auto_open = True, - label = 'name', - menu=Menu( NewAction, - Separator(), - def_title_action, - dept_action, - Separator(), - CopyAction, - CutAction, - PasteAction, - Separator(), - DeleteAction, - Separator(), - RenameAction ), - view = employee_view ) - ] - ) - - # The main view - view = View( - Group( - Item( - name = 'company', - id = 'company', - editor = tree_editor, - resizable = True ), - orientation = 'vertical', - show_labels = True, - show_left = True, ), - title = 'Company Structure', - id = \ - 'traitsui.tests.tree_editor_test', - dock = 'horizontal', - drop_class = HasTraits, - handler = TreeHandler(), - buttons = [ 'Undo', 'OK', 'Cancel' ], - resizable = True, - width = .3, - height = .3 ) - - if __name__ == '__main__': - owner.configure_traits( view = view ) +.. literalinclude:: examples/tree_editor.py + :start-at: tree_editor.py .. _defining-nodes: @@ -963,13 +708,19 @@ To define a node type without children, set the **children** attribute of TreeNode to the empty string. In Example 16, the following lines define the node type for the node that displays the company name, with no children:: - TreeNode( node_for = [ Company ], - auto_open = True, - children = '', - label = 'name', - view = View( Group('name', - orientation='vertical', - show_left=True )) ), + TreeNode( + node_for=[Company], + auto_open=True, + children='', + label='name', + view=View( + Group( + 'name', + orientation='vertical', + show_left=True, + ), + ), + ) .. _a-node-type-with-children: @@ -985,12 +736,14 @@ trait attribute of Company. :: - TreeNode( node_for = [ Company ], - auto_open = True, - children = 'departments', - label = '=Departments', - view = no_view, - add = [ Department ] ), + TreeNode( + node_for=[Company], + auto_open=True, + children='departments', + label='=Departments', + view=no_view, + add=[Department], + ) .. _setting-the-label-of-a-tree-node: @@ -1031,23 +784,31 @@ containing Action objects for the menu commands. In Example 16, the following lines define the node type for employees, including a shortcut menu for employee nodes:: - TreeNode( node_for = [ Department ], - auto_open = True, - children = 'employees', - label = 'name', - menu = Menu( NewAction, - Separator(), - DeleteAction, - Separator(), - RenameAction, - Separator(), - CopyAction, - CutAction, - PasteAction ), - view = View( Group ('name', - orientation='vertical', - show_left=True )), - add = [ Employee ] ), + TreeNode( + node_for=[Department], + auto_open=True, + children='employees', + label='name', + menu=Menu( + NewAction, + Separator(), + DeleteAction, + Separator(), + RenameAction, + Separator(), + CopyAction, + CutAction, + PasteAction, + ), + view=View( + Group( + 'name', + orientation='vertical', + show_left=True, + ), + ), + add=[Employee], + ), .. _allowing-the-hierarchy-to-be-modified: @@ -1177,18 +938,20 @@ following: For example:: - shared_tree_1 = TreeEditor(shared_editor = True, - editor = my_shared_editor_pane, - nodes = [ TreeNode( # ... - ) - ] - ) - shared_tree_2 = TreeEditor(shared_editor = True, - editor = my_shared_editor_pane, - nodes = [ TreeNode( # ... - ) - ] - ) + shared_tree_1 = TreeEditor( + shared_editor=True, + editor=my_shared_editor_pane, + nodes=[ + TreeNode(...), + ], + ) + shared_tree_2 = TreeEditor( + shared_editor=True, + editor=my_shared_editor_pane, + nodes=[ + TreeNode(...), + ], + ) .. _tree-defining-the-format: diff --git a/docs/source/traitsui_user_manual/factories_basic.rst b/docs/source/traitsui_user_manual/factories_basic.rst index 05f5bf742..0e637c67f 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -63,40 +63,8 @@ The following code generates the editors shown in Figure 21. .. rubric:: Example 14: Demonstration of array editors -:: - - # array_editor.py -- Example of using array editors - - import numpy as np - from traits.api import HasPrivateTraits, Array - from traitsui.api \ - import View, ArrayEditor, Item - from traitsui.menu import NoButtons - - class ArrayEditorTest ( HasPrivateTraits ): - - three = Array(np.int, (3,3)) - four = Array(np.float, - (4,4), - editor = ArrayEditor(width = -50)) - - view = View( Item('three', label='3x3 Integer'), - '_', - Item('three', - label='Integer Read-only', - style='readonly'), - '_', - Item('four', label='4x4 Float'), - '_', - Item('four', - label='Float Read-only', - style='readonly'), - buttons = NoButtons, - resizable = True ) - - - if __name__ == '__main__': - ArrayEditorTest().configure_traits() +.. literalinclude:: examples/array_editor.py + :start-at: array_editor.py BooleanEditor() ``````````````` @@ -130,7 +98,7 @@ text input in place of that value. For example, to create a Boolean editor that accepts only yes and no as appropriate text values, you might use the following expression:: - editor=BooleanEditor(mapping={"yes":True, "no":False}) + editor=BooleanEditor(mapping={"yes": True, "no": False}) Note that in this case, the strings True and False would *not* be acceptable as text input. @@ -546,28 +514,8 @@ tags, and then strips out the tags. .. rubric:: Example 15: Enumeration editor with mapped values -:: - - # enum_editor.py -- Example of using an enumeration editor - from traits.api import HasTraits, Enum - from traitsui.api import EnumEditor - - Class EnumExample(HasTraits): - priority = Enum('Medium', 'Highest', - 'High', - 'Medium', - 'Low', - 'Lowest') - - view = View( Item(name='priority', - editor=EnumEditor(values={ - 'Highest' : '1:Highest', - 'High' : '2:High', - 'Medium' : '3:Medium', - 'Low' : '4:Low', - 'Lowest' : '5:Lowest', }))) - - EnumExample().configure_traits() +.. literalinclude:: examples/enum_editor.py + :start-at: enum_editor.py The enumeration editor strips the characters up to and including the colon. It assumes that all the items have the colon in the same position; therefore, if @@ -904,64 +852,8 @@ pick a captain and edit that person's information. Example 16: Instance editor with instance selection -:: - - # instance_editor_selection.py -- Example of an instance editor - # with instance selection - - from traits.api \ - import HasStrictTraits, Int, Instance, List, Regex, Str - from traitsui.api \ - import View, Item, InstanceEditor - - class Person(HasStrictTraits): - name = Str() - age = Int() - phone = Regex( - value = '000-0000', - regex = '\d\d\d[-]\d\d\d\d', - ) - - traits_view = View('name', 'age', 'phone') - - people = [ - Person(name= 'Dave', age=39, phone='555-1212'), - Person(name='Mike', age=28, phone='555-3526'), - Person(name='Joe', age=34, phone='555-6943'), - Person(name='Tom', age=22, phone='555-7586'), - Person(name='Dick', age=63, phone='555-3895'), - Person(name='Harry' age=46, phone='555-3285'), - Person(name='Sally', age=43, phone='555-8797'), - Person(name='Fields', age=31, phone='555-3547') - ] - - class Team(HasStrictTraits): - - name = Str() - captain = Instance(Person) - roster = List(Person) - - traits_view = View( - Item('name'), - Item('_'), - Item( - 'captain', - label='Team Captain', - editor = InstanceEditor( - name = 'roster', - editable = True), - style = 'custom', - ), - buttons = ['OK'] - ) - - if __name__ == '__main__': - team = Team( - name = 'Vultures', - captain = people[0], - roster = people - ) - team.configure_traits() +.. literalinclude:: examples/instance_editor_selection.py + :start-at: instance_editor_selection.py .. figure:: images/ui_for_ex16.png :alt: Dialog box for a "team", with drop-list selection for "Team Captain" diff --git a/docs/source/traitsui_user_manual/factory_intro.rst b/docs/source/traitsui_user_manual/factory_intro.rst index 5a561f034..dc9d802be 100644 --- a/docs/source/traitsui_user_manual/factory_intro.rst +++ b/docs/source/traitsui_user_manual/factory_intro.rst @@ -33,32 +33,8 @@ Consider the following script and the window it creates: .. rubric:: Example 12: Using default trait editors -:: - - # default_trait_editors.py -- Example of using default - # trait editors - - from traits.api import HasTraits, Str, Range, Bool - from traitsui.api import View, Item - - class Adult(HasTraits): - first_name = Str() - last_name = Str() - age = Range(21,99) - registered_voter = Bool() - - - traits_view = View(Item(name='first_name'), - Item(name='last_name'), - Item(name='age'), - Item(name='registered_voter')) - - alice = Adult(first_name='Alice', - last_name='Smith', - age=42, - registered_voter=True) - - alice.configure_traits() +.. literalinclude:: examples/default_trait_editors.py + :start-at: default_trait_editors.py .. figure:: images/ui_for_ex12.jpg :alt: UI showing text boxes for names, slider for Age, and checkbox for voter @@ -180,7 +156,12 @@ selecting elements from a specified set; the contents of this set must, of course, be known to the editor. This sort of initialization is usually performed by means of one or more keyword arguments to the editor factory, for example:: - Item(name='my_list',editor=CheckListEditor(values=["opt1","opt2","opt3"])) + Item( + name='my_list', + editor=CheckListEditor( + values=["opt1","opt2","opt3"], + ), + ) The descriptions of trait editor factories in :ref:`the-predefined-trait-editor-factories` include a list of required and @@ -329,34 +310,8 @@ example, consider the following script: .. rubric:: Example 13: Using editor styles at various levels -:: - - # mixed_styles.py -- Example of using editor styles at - # various levels - - from traits.api import HasTraits, Str, Enum - from traitsui.api import View, Group, Item - - class MixedStyles(HasTraits): - first_name = Str() - last_name = Str() - - department = Enum("Business", "Research", "Admin") - position_type = Enum("Full-Time", - "Part-Time", - "Contract") - - traits_view = View(Group(Item(name='first_name'), - Item(name='last_name'), - Group(Item(name='department'), - Item(name='position_type', - style='custom'), - style='simple')), - title='Mixed Styles', - style='readonly') - - ms = MixedStyles(first_name='Sam', last_name='Smith') - ms.configure_traits() +.. literalinclude:: examples/mixed_styles.py + :start-at: mixed_styles.py Notice how the editor styles are set for each attribute: diff --git a/docs/source/traitsui_user_manual/handler.rst b/docs/source/traitsui_user_manual/handler.rst index 7dc22c572..18bd798f1 100644 --- a/docs/source/traitsui_user_manual/handler.rst +++ b/docs/source/traitsui_user_manual/handler.rst @@ -430,7 +430,7 @@ argument):: #created else: #code to be executed only when 'foo' changes after - #window initialization} + #window initialization #code to be executed in either case @@ -442,38 +442,8 @@ overridden setattr() method and user interface notification method. .. rubric:: Example 9: Using a Handler that reacts to trait changes -:: - - # handler_override.py -- Example of a Handler that overrides - # setattr(), and that has a user interface - # notification method - - from traits.api import HasTraits, Bool - from traitsui.api import View, Handler - - class TC_Handler(Handler): - - def setattr(self, info, object, name, value): - Handler.setattr(self, info, object, name, value) - info.object._updated = True - - def object__updated_changed(self, info): - if info.initialized: - info.ui.title += "*" - - class TestClass(HasTraits): - b1 = Bool() - b2 = Bool() - b3 = Bool() - _updated = Bool(False) - - view1 = View('b1', 'b2', 'b3', - title="Alter Title", - handler=TC_Handler(), - buttons = ['OK', 'Cancel']) - - tc = TestClass() - tc.configure_traits(view=view1) +.. literalinclude:: examples/handler_override.py + :start-at: handler_override.py .. image:: images/alter_title_before.png :alt: Dialog box with empty checkboxes and a title of "Alter Title" @@ -508,8 +478,7 @@ the logic for the window. To create the action: UIInfo object. #. Create an Action instance using the name of the new method, e.g.:: - recalc = Action(name = "Recalculate", - action = "do_recalc") + recalc = Action(name="Recalculate", action="do_recalc") .. _custom-command-buttons: @@ -523,9 +492,11 @@ along with any standard buttons you specify. #. Define the handler method and action, as described in :ref:`actions`. #. Include the new Action in the **buttons** attribute for the View:: - View ( #view contents, - # ..., - buttons = [ OKButton, CancelButton, recalc ]) + View( + # view contents, + # ..., + buttons=[OKButton, CancelButton, recalc], + ) .. _menus-and-menu-bars: @@ -545,11 +516,13 @@ make it into a menu option. These steps can be executed all at once when the View is created, as in the following code:: - View ( #view contents, - # ..., - menubar = MenuBar( - Menu( my_action, - name = 'My Special Menu'))) + View( + # view contents, + # ..., + menubar=MenuBar( + Menu(my_action, name='My Special Menu'), + ), + ) .. _toolbars: @@ -568,10 +541,12 @@ except that toolbars do not contain menus; they directly contain actions. From pyface.api import ImageResource - recalc = Action(name = "Recalculate", - action = "do_recalc", - toolip = "Recalculate the results", - image = ImageResource("recalc.png")) + recalc = Action( + name="Recalculate", + action="do_recalc", + toolip="Recalculate the results", + image=ImageResource("recalc.png"), + ) 2. If the View does not already include a ToolBar, create one and assign it to the View's **toolbar** attribute. @@ -580,9 +555,11 @@ except that toolbars do not contain menus; they directly contain actions. As with a MenuBar, these steps can be executed all at once when the View is created, as in the following code:: - View ( #view contents, - # ..., - toolbar = ToolBar(my_action)) + View( + # view contents, + # ..., + toolbar=ToolBar(my_action), + ) .. _undo_redo: diff --git a/docs/source/traitsui_user_manual/view.rst b/docs/source/traitsui_user_manual/view.rst index 81eeaec6c..3dc3598e9 100644 --- a/docs/source/traitsui_user_manual/view.rst +++ b/docs/source/traitsui_user_manual/view.rst @@ -21,10 +21,9 @@ object: :: - # configure_traits.py -- Sample code to demonstrate - # configure_traits() - from traits.api import HasTraits, Str, Int - import traitsui + # configure_traits.py -- Sample code to demonstrate configure_traits() + from traits.api import HasTraits, Int, Str + class SimpleEmployee(HasTraits): first_name = Str() @@ -34,6 +33,7 @@ object: employee_number = Str() salary = Int() + sam = SimpleEmployee() sam.configure_traits() @@ -72,28 +72,8 @@ View object and passing it to the configure_traits() method: .. rubric:: Example 2: Using configure_traits() with a View object -:: - - # configure_traits_view.py -- Sample code to demonstrate - # configure_traits() - - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item - import traitsui - - class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - employee_number = Str() - salary = Int() - - view1 = View(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department')) - - sam = SimpleEmployee() - sam.configure_traits(view=view1) +.. literalinclude:: examples/configure_traits_view.py + :start-at: configure_traits_view.py The resulting window has the desired appearance: @@ -347,30 +327,8 @@ Consider the following enhancement to Example 2: .. rubric:: Example 3: Using configure_traits() with a View and a Group object -:: - - # configure_traits_view_group.py -- Sample code to demonstrate - # configure_traits() - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item, Group - import traitsui - - class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - view1 = View(Group(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - label = 'Personnel profile', - show_border = True)) - - sam = SimpleEmployee() - sam.configure_traits(view=view1) +.. literalinclude:: examples/configure_traits_view_group.py + :start-at: configure_traits_view_group.py The resulting window shows the same widgets as before, but they are now enclosed in a visible border with a text label: diff --git a/examples/tutorials/doc_examples/examples/array_editor.py b/examples/tutorials/doc_examples/examples/array_editor.py deleted file mode 100644 index 386669538..000000000 --- a/examples/tutorials/doc_examples/examples/array_editor.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# array_editor.py -- Example of using array editors - -# --[Imports]-------------------------------------------------------------- -from numpy import int16, float32 - -from traits.api import HasPrivateTraits, Array -from traitsui.api import View, ArrayEditor, Item -from traitsui.menu import NoButtons - -# --[Code]----------------------------------------------------------------- - - -class ArrayEditorTest(HasPrivateTraits): - - three = Array(int16, (3, 3)) - four = Array(float32, - (4, 4), - editor=ArrayEditor(width=-50)) - - view = View(Item('three', label='3x3 Integer'), - '_', - Item('three', - label='Integer Read-only', - style='readonly'), - '_', - Item('four', label='4x4 Float'), - '_', - Item('four', - label='Float Read-only', - style='readonly'), - buttons=NoButtons, - resizable=True) - - -if __name__ == '__main__': - ArrayEditorTest().configure_traits() diff --git a/examples/tutorials/doc_examples/examples/configure_traits_view.py b/examples/tutorials/doc_examples/examples/configure_traits_view.py deleted file mode 100644 index 9b07ea7df..000000000 --- a/examples/tutorials/doc_examples/examples/configure_traits_view.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - - -# configure_traits_view.py -- Sample code to demonstrate configure_traits() - - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item - -# --[Code]----------------------------------------------------------------- - - -class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - employee_number = Str() - salary = Int() - - -view1 = View(Item(name='first_name'), - Item(name='last_name'), - Item(name='department')) - -sam = SimpleEmployee() -sam.configure_traits(view=view1) diff --git a/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py b/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py deleted file mode 100644 index 9494c60a0..000000000 --- a/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - - -# configure_traits_view_buttons.py -- Sample code to demonstrate -# configure_traits() - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item -from traitsui.menu import OKButton, CancelButton - -# --[Code]----------------------------------------------------------------- - - -class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - -view1 = View(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - buttons=[OKButton, CancelButton]) - -sam = SimpleEmployee() -sam.configure_traits(view=view1) diff --git a/examples/tutorials/doc_examples/examples/configure_traits_view_group.py b/examples/tutorials/doc_examples/examples/configure_traits_view_group.py deleted file mode 100644 index 5c5487a74..000000000 --- a/examples/tutorials/doc_examples/examples/configure_traits_view_group.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - - -# configure_traits_view_group.py -- Sample code to demonstrate -# configure_traits() - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item, Group - -# --[Code]----------------------------------------------------------------- - - -class SimpleEmployee(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - -view1 = View(Group(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - label='Personnel profile', - show_border=True)) - - -sam = SimpleEmployee() -sam.configure_traits(view=view1) diff --git a/examples/tutorials/doc_examples/examples/default_trait_editors.py b/examples/tutorials/doc_examples/examples/default_trait_editors.py deleted file mode 100644 index 764bfd34d..000000000 --- a/examples/tutorials/doc_examples/examples/default_trait_editors.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# default_trait_editors.py -- Example of using default trait editors - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Range, Bool -from traitsui.api import View, Item - -# --[Code]----------------------------------------------------------------- - - -class Adult(HasTraits): - first_name = Str() - last_name = Str() - age = Range(21, 99) - registered_voter = Bool() - - traits_view = View(Item(name='first_name'), - Item(name='last_name'), - Item(name='age'), - Item(name='registered_voter')) - - -alice = Adult(first_name='Alice', - last_name='Smith', - age=42, - registered_voter=True) - -alice.configure_traits() diff --git a/examples/tutorials/doc_examples/examples/default_traits_view.py b/examples/tutorials/doc_examples/examples/default_traits_view.py deleted file mode 100644 index b87f9880c..000000000 --- a/examples/tutorials/doc_examples/examples/default_traits_view.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# default_traits_view.py -- Sample code to demonstrate the use of 'traits_view' - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item, Group - -# --[Code]----------------------------------------------------------------- - - -class SimpleEmployee2(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - traits_view = View(Group(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - label='Personnel profile', - show_border=True)) - - -sam = SimpleEmployee2() -sam.configure_traits() diff --git a/examples/tutorials/doc_examples/examples/enum_editor.py b/examples/tutorials/doc_examples/examples/enum_editor.py deleted file mode 100644 index d114540a8..000000000 --- a/examples/tutorials/doc_examples/examples/enum_editor.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# enum_editor.py -- Example of using an enumeration editor - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Enum -from traitsui.api import EnumEditor, View, Item - -# --[Code]----------------------------------------------------------------- - - -class EnumExample(HasTraits): - priority = Enum('Medium', 'Highest', - 'High', - 'Medium', - 'Low', - 'Lowest') - - view = View(Item(name='priority', - editor=EnumEditor(values={ - 'Highest': '1:Highest', - 'High': '2:High', - 'Medium': '3:Medium', - 'Low': '4:Low', - 'Lowest': '5:Lowest', }))) - - -EnumExample().configure_traits() diff --git a/examples/tutorials/doc_examples/examples/handler_override.py b/examples/tutorials/doc_examples/examples/handler_override.py deleted file mode 100644 index 9d558df94..000000000 --- a/examples/tutorials/doc_examples/examples/handler_override.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# handler_override.py -- Example of a Handler that overrides setattr(), and -# that has a user interface notification method - -# --[Imports]-------------------------------------------------------------- - -from traits.api import HasTraits, Bool -from traitsui.api import View, Handler - -# --[Code]----------------------------------------------------------------- - - -class TC_Handler(Handler): - - def setattr(self, info, object, name, value): - Handler.setattr(self, info, object, name, value) - info.object._updated = True - - def object__updated_changed(self, info): - if info.initialized: - info.ui.title += "*" - - -class TestClass(HasTraits): - b1 = Bool() - b2 = Bool() - b3 = Bool() - _updated = Bool(False) - - -view1 = View('b1', 'b2', 'b3', - title="Alter Title", - handler=TC_Handler(), - buttons=['OK', 'Cancel']) - -tc = TestClass() -tc.configure_traits(view=view1) diff --git a/examples/tutorials/doc_examples/examples/instance_editor_selection.py b/examples/tutorials/doc_examples/examples/instance_editor_selection.py deleted file mode 100644 index f7014835c..000000000 --- a/examples/tutorials/doc_examples/examples/instance_editor_selection.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# instance_editor_selection.py -- Example of an instance editor with -# instance selection - -# --[Imports]-------------------------------------------------------------- - -from traits.api \ - import HasStrictTraits, Int, Instance, List, Regex, Str -from traitsui.api \ - import View, Item, InstanceEditor - -# --[Code]----------------------------------------------------------------- - - -class Person(HasStrictTraits): - name = Str() - age = Int() - phone = Regex(value='000-0000', - regex=r'\d\d\d[-]\d\d\d\d') - - traits_view = View('name', 'age', 'phone') - - -people = [ - Person(name='Dave', age=39, phone='555-1212'), - Person(name='Mike', age=28, phone='555-3526'), - Person(name='Joe', age=34, phone='555-6943'), - Person(name='Tom', age=22, phone='555-7586'), - Person(name='Dick', age=63, phone='555-3895'), - Person(name='Harry', age=46, phone='555-3285'), - Person(name='Sally', age=43, phone='555-8797'), - Person(name='Fields', age=31, phone='555-3547') -] - - -class Team(HasStrictTraits): - - name = Str() - captain = Instance(Person) - roster = List(Person) - - traits_view = View(Item('name'), - Item('_'), - Item('captain', - label='Team Captain', - editor=InstanceEditor(name='roster', - editable=True), - style='custom', - ), - buttons=['OK']) - -# --[Example*]------------------------------------------------------------- - - -if __name__ == '__main__': - Team(name='Vultures', - captain=people[0], - roster=people).configure_traits() diff --git a/examples/tutorials/doc_examples/examples/key_bindings.py b/examples/tutorials/doc_examples/examples/key_bindings.py deleted file mode 100644 index 589fc9d5a..000000000 --- a/examples/tutorials/doc_examples/examples/key_bindings.py +++ /dev/null @@ -1,73 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# key_bindings.py -- Example of a code editor with a key bindings editor - -# --[Imports]-------------------------------------------------------------- -from traits.api \ - import Button, Code, HasPrivateTraits, Str - -from traitsui.api \ - import View, Item, Group, Handler - -from traitsui.key_bindings \ - import KeyBinding, KeyBindings - -# --[Code]----------------------------------------------------------------- - -key_bindings = KeyBindings( - KeyBinding(binding1='Ctrl-s', - description='Save to a file', - method_name='save_file'), - KeyBinding(binding1='Ctrl-r', - description='Run script', - method_name='run_script'), - KeyBinding(binding1='Ctrl-k', - description='Edit key bindings', - method_name='edit_bindings') -) - -# Traits UI Handler class for bound methods - - -class CodeHandler(Handler): - - def save_file(self, info): - info.object.status = "save file" - - def run_script(self, info): - info.object.status = "run script" - - def edit_bindings(self, info): - info.object.status = "edit bindings" - key_bindings.edit_traits() - - -class KBCodeExample(HasPrivateTraits): - - code = Code() - status = Str() - kb = Button(label='Edit Key Bindings') - - view = View(Group( - Item('code', - style='custom', - resizable=True), - Item('status', style='readonly'), - 'kb', - orientation='vertical', - show_labels=False, - ), - id='KBCodeExample', - key_bindings=key_bindings, - title='Code Editor With Key Bindings', - resizable=True, - - handler=CodeHandler()) - - def _kb_fired(self, event): - key_bindings.edit_traits() - - -if __name__ == '__main__': - KBCodeExample().configure_traits() diff --git a/examples/tutorials/doc_examples/examples/mixed_styles.py b/examples/tutorials/doc_examples/examples/mixed_styles.py deleted file mode 100644 index 0990800b5..000000000 --- a/examples/tutorials/doc_examples/examples/mixed_styles.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# mixed_styles.py -- Example of using editor styles at various levels - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Enum -from traitsui.api import View, Group, Item - -# --[Code]----------------------------------------------------------------- - - -class MixedStyles(HasTraits): - first_name = Str() - last_name = Str() - - department = Enum("Business", "Research", "Admin") - position_type = Enum("Full-Time", - "Part-Time", - "Contract") - - traits_view = View(Group(Item(name='first_name'), - Item(name='last_name'), - Group(Item(name='department'), - Item(name='position_type', - style='custom'), - style='simple')), - title='Mixed Styles', - style='readonly') - - -ms = MixedStyles(first_name='Sam', last_name='Smith') -ms.configure_traits() diff --git a/examples/tutorials/doc_examples/examples/multi_object_view.py b/examples/tutorials/doc_examples/examples/multi_object_view.py deleted file mode 100644 index a653d37d6..000000000 --- a/examples/tutorials/doc_examples/examples/multi_object_view.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# multi_object_view.py -- Sample code to show multi-object view with context - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int, Bool -from traitsui.api import View, Group, Item - -# --[Code]----------------------------------------------------------------- - -# Sample class - - -class House(HasTraits): - address = Str() - bedrooms = Int() - pool = Bool() - price = Int() - - -# View object designed to display two objects of class 'House' -comp_view = View( - Group( - Group( - Item('h1.address', resizable=True), - Item('h1.bedrooms'), - Item('h1.pool'), - Item('h1.price'), - show_border=True - ), - Group( - Item('h2.address', resizable=True), - Item('h2.bedrooms'), - Item('h2.pool'), - Item('h2.price'), - show_border=True - ), - orientation='horizontal' - ), - title='House Comparison' -) - -# A pair of houses to demonstrate the View -house1 = House(address='4743 Dudley Lane', - bedrooms=3, - pool=False, - price=150000) -house2 = House(address='11604 Autumn Ridge', - bedrooms=3, - pool=True, - price=200000) - -# ...And the actual display command -house1.configure_traits(view=comp_view, context={'h1': house1, 'h2': house2}) diff --git a/examples/tutorials/doc_examples/examples/multiple_views.py b/examples/tutorials/doc_examples/examples/multiple_views.py deleted file mode 100644 index b63b396e4..000000000 --- a/examples/tutorials/doc_examples/examples/multiple_views.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# multiple_views.py -- Sample code to demonstrate the use of multiple views - -# --[Imports]-------------------------------------------------------------- -from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item, Group - -# --[Code]----------------------------------------------------------------- - - -class SimpleEmployee3(HasTraits): - first_name = Str() - last_name = Str() - department = Str() - - employee_number = Str() - salary = Int() - - traits_view = View(Group(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - label='Personnel profile', - show_border=True)) - - all_view = View(Group(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - Item(name='employee_number'), - Item(name='salary'), - label='Personnel database ' + - 'entry', - show_border=True)) - - -sam = SimpleEmployee3() -sam.configure_traits() -sam.configure_traits(view='all_view') diff --git a/examples/tutorials/doc_examples/examples/tree_editor.py b/examples/tutorials/doc_examples/examples/tree_editor.py deleted file mode 100644 index 25f199127..000000000 --- a/examples/tutorials/doc_examples/examples/tree_editor.py +++ /dev/null @@ -1,206 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# tree_editor.py -- Example of a tree editor - -# --[Imports]-------------------------------------------------------------- - -from traits.api \ - import HasTraits, Str, Regex, List, Instance -from traitsui.api \ - import TreeEditor, TreeNode, View, Item, VSplit, \ - HGroup, Handler, Group -from traitsui.menu \ - import Menu, Action, Separator -try: - from traitsui.wx.tree_editor \ - import NewAction, CopyAction, CutAction, \ - PasteAction, DeleteAction, RenameAction -except RuntimeError: - from traitsui.qt4.tree_editor \ - import NewAction, CopyAction, CutAction, \ - PasteAction, DeleteAction, RenameAction -# --[Code]----------------------------------------------------------------- - -# DATA CLASSES - - -class Employee(HasTraits): - name = Str('') - title = Str() - phone = Regex(regex=r'\d\d\d-\d\d\d\d') - - def default_title(self): - self.title = 'Senior Engineer' - - -class Department(HasTraits): - name = Str('') - employees = List(Employee) - - -class Company(HasTraits): - name = Str('') - departments = List(Department) - employees = List(Employee) - - -class Owner(HasTraits): - name = Str('') - company = Instance(Company) - -# INSTANCES - - -jason = Employee( - name='Jason', - title='Engineer', - phone='536-1057') - -mike = Employee( - name='Mike', - title='Sr. Marketing Analyst', - phone='536-1057') - -dave = Employee( - name='Dave', - title='Sr. Engineer', - phone='536-1057') - -susan = Employee( - name='Susan', - title='Engineer', - phone='536-1057') - -betty = Employee( - name='Betty', - title='Marketing Analyst') - -owner = Owner( - name='wile', - company=Company( - name='Acme Labs, Inc.', - departments=[ - Department( - name='Marketing', - employees=[mike, betty] - ), - Department( - name='Engineering', - employees=[dave, susan, jason] - ) - ], - employees=[dave, susan, mike, betty, jason] - ) -) - -# View for objects that aren't edited -no_view = View() - -# Actions used by tree editor context menu - -def_title_action = Action(name='Default title', - action='object.default') - -dept_action = Action( - name='Department', - action='handler.employee_department(editor,object)') - -# View used by tree editor -employee_view = View( - VSplit( - HGroup('3', 'name'), - HGroup('9', 'title'), - HGroup('phone'), - id='vsplit'), - id='traits.doc.example.treeeditor', - dock='vertical') - - -class TreeHandler(Handler): - - def employee_department(self, editor, object): - dept = editor.get_parent(object) - print('%s works in the %s department.' % (object.name, dept.name)) - - -# Tree editor -tree_editor = TreeEditor( - nodes=[ - TreeNode(node_for=[Company], - auto_open=True, - children='', - label='name', - view=View(Group('name', - orientation='vertical', - show_left=True))), - TreeNode(node_for=[Company], - auto_open=True, - children='departments', - label='=Departments', - view=no_view, - add=[Department]), - TreeNode(node_for=[Company], - auto_open=True, - children='employees', - label='=Employees', - view=no_view, - add=[Employee]), - TreeNode(node_for=[Department], - auto_open=True, - children='employees', - label='name', - menu=Menu(NewAction, - Separator(), - DeleteAction, - Separator(), - RenameAction, - Separator(), - CopyAction, - CutAction, - PasteAction), - view=View(Group('name', - orientation='vertical', - show_left=True)), - add=[Employee]), - TreeNode(node_for=[Employee], - auto_open=True, - label='name', - menu=Menu(NewAction, - Separator(), - def_title_action, - dept_action, - Separator(), - CopyAction, - CutAction, - PasteAction, - Separator(), - DeleteAction, - Separator(), - RenameAction), - view=employee_view) - ] -) -# The main view -view = View( - Group( - Item( - name='company', - id='company', - editor=tree_editor, - resizable=True), - orientation='vertical', - show_labels=True, - show_left=True, ), - title='Company Structure', - id='traitsui.tests.tree_editor_test', - dock='horizontal', - drop_class=HasTraits, - handler=TreeHandler(), - buttons=['Undo', 'OK', 'Cancel'], - resizable=True, - width=.3, - height=.3) - -if __name__ == '__main__': - owner.configure_traits(view=view) diff --git a/examples/tutorials/doc_examples/examples/wizard.py b/examples/tutorials/doc_examples/examples/wizard.py deleted file mode 100644 index 96ea299cf..000000000 --- a/examples/tutorials/doc_examples/examples/wizard.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - -# wizard.py ---Example of a traits-based wizard UI - -from traits.api import HasTraits, Str -from traits.etsconfig.api import ETSConfig -from traitsui.api import Item, View, VGroup - - -class Person(HasTraits): - first_name = Str() - last_name = Str() - - company = Str() - position = Str() - - view = View( - VGroup( - Item("first_name"), - Item("last_name"), - # label="Personal info" - ), - VGroup( - Item("company"), - Item("position"), - # label="Professional info" - ) - - ) - - -person = Person(first_name='Postman', last_name='Pat', company="Enthought", - position="Software Developer") - - -if ETSConfig.toolkit == "wx": - # Wizard window is currently only available for wx backend. - person.configure_traits(kind='wizard') diff --git a/integrationtests/test_all_examples.py b/integrationtests/test_all_examples.py index 29682aca3..7bfefa800 100644 --- a/integrationtests/test_all_examples.py +++ b/integrationtests/test_all_examples.py @@ -203,10 +203,6 @@ def get_python_files(self): os.path.join(TUTORIALS, "view_standalone.py"), lambda: True, "Require wx and is blocking.", ) -SEARCHER.skip_file_if( - os.path.join(TUTORIALS, "wizard.py"), - is_qt, "Failing on Qt, see enthought/traitsui#773", -) # Validate configuration. SEARCHER.validate()