From e2f6da9eaa2c6bee0026c40a36dea0ca3412f35d Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:12:46 +0530 Subject: [PATCH 01/18] CLN : Format code examples in docs modified: docs/source/traitsui_user_manual/adapters.rst modified: docs/source/traitsui_user_manual/advanced_view.rst modified: docs/source/traitsui_user_manual/custom_view.rst modified: docs/source/traitsui_user_manual/factories_advanced_extra.rst modified: docs/source/traitsui_user_manual/factories_basic.rst modified: docs/source/traitsui_user_manual/factory_intro.rst modified: docs/source/traitsui_user_manual/handler.rst modified: docs/source/traitsui_user_manual/view.rst --- docs/source/traitsui_user_manual/adapters.rst | 14 +- .../traitsui_user_manual/advanced_view.rst | 130 +++-- .../traitsui_user_manual/custom_view.rst | 66 +-- .../factories_advanced_extra.rst | 488 ++++++++++-------- .../traitsui_user_manual/factories_basic.rst | 115 +++-- .../traitsui_user_manual/factory_intro.rst | 69 +-- docs/source/traitsui_user_manual/handler.rst | 62 ++- docs/source/traitsui_user_manual/view.rst | 38 +- 8 files changed, 551 insertions(+), 431 deletions(-) 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..4446763b1 100644 --- a/docs/source/traitsui_user_manual/advanced_view.rst +++ b/docs/source/traitsui_user_manual/advanced_view.rst @@ -66,7 +66,7 @@ variation on Example 3: :: # default_traits_view.py -- Sample code to demonstrate the use of - # 'traits_view' + # 'traits_view' from traits.api import HasTraits, Str, Int from traitsui.api import View, Item, Group import traitsui @@ -79,11 +79,15 @@ variation on Example 3: 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)) + 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() @@ -118,7 +122,7 @@ example above would be implemented as follows: :: # default_traits_view2.py -- Sample code to demonstrate the use of - # 'default_traits_view' + # 'default_traits_view' from traits.api import HasTraits, Str, Int from traitsui.api import View, Item, Group import traitsui @@ -132,11 +136,15 @@ example above would be implemented as follows: 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)) + 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() @@ -168,7 +176,7 @@ this by simply adding a second View attribute: :: # multiple_views.py -- Sample code to demonstrate the use of - # multiple views + # multiple views from traits.api import HasTraits, Str, Int from traitsui.api import View, Item, Group import traitsui @@ -181,20 +189,27 @@ this by simply adding a second View attribute: 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)) + 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() @@ -240,9 +255,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. @@ -390,7 +411,7 @@ example shows: :: # multi_object_view.py -- Sample code to show multi-object view - # with context + # with context from traits.api import HasTraits, Str, Int, Bool from traitsui.api import View, Group, Item @@ -424,18 +445,23 @@ example shows: 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) + 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}) + house1.configure_traits( + view=comp_view, context={'h1':house1, 'h2':house2}, + ) .. FIXME: This is a bit assymmetrical. Can we clean it up without complicating the example overly? @@ -505,21 +531,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..ab7b15e1b 100644 --- a/docs/source/traitsui_user_manual/custom_view.rst +++ b/docs/source/traitsui_user_manual/custom_view.rst @@ -103,32 +103,36 @@ pages that a user must navigate sequentially. :: - # wizard.py ---Example of a traits-based wizard UI + # wizard.py ---Example of a traits-based wizard UI - from traits.api import HasTraits, Str - from traitsui.api import Item, View, VGroup + 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") - ) - ) + class Person(HasTraits): + first_name = Str() + last_name = Str() - person = Person(first_name='Postman', last_name='Pat', company="Enthought", - position="Software Developer") - person.configure_traits(kind='wizard') + 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') leads to the following 2 modal dialogs: @@ -197,7 +201,7 @@ Consider the following variation on Example 3: :: # configure_traits_view_buttons.py -- Sample code to demonstrate - # configure_traits() + # configure_traits() from traits.api import HasTraits, Str, Int from traitsui.api import View, Item @@ -211,10 +215,12 @@ Consider the following variation on Example 3: employee_number = Str() salary = Int() - view1 = View(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department'), - buttons = [OKButton, CancelButton]) + view1 = View( + Item(name = 'first_name'), + Item(name = 'last_name'), + Item(name = 'department'), + buttons=[OKButton, CancelButton], + ) sam = SimpleEmployee() sam.configure_traits(view=view1) @@ -266,7 +272,9 @@ 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/factories_advanced_extra.rst b/docs/source/traitsui_user_manual/factories_advanced_extra.rst index e0f7e3596..1749125ff 100644 --- a/docs/source/traitsui_user_manual/factories_advanced_extra.rst +++ b/docs/source/traitsui_user_manual/factories_advanced_extra.rst @@ -131,65 +131,66 @@ with associated key bindings, and a button that invokes the key binding editor. :: # key_bindings.py -- Example of a code editor with a - # key bindings editor + # 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 + from traits.api import Button, Code, HasPrivateTraits, Str + from traitsui.api import CodeEditor, 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' ) + 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 ): + class CodeHandler(Handler): - def save_file ( self, info ): + def save_file(self, info): info.object.status = "save file" - def run_script ( self, info ): + def run_script(self, info): info.object.status = "run script" - def edit_bindings ( self, info ): + def edit_bindings(self, info): info.object.status = "edit bindings" key_bindings.edit_traits() - class KBCodeExample ( HasPrivateTraits ): + 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() + 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() @@ -746,196 +747,225 @@ The following example shows the code that produces the editor shown in Figure # 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 + from traits.api import HasTraits, Instance, List, Str, Regex + from traitsui.api import ( + Group, Handler, HGroup, Item, TreeEditor, TreeNode, View, VSplit, + ) + from traitsui.menu import Action, Menu, Separator + from traitsui.wx.tree_editor import ( + CopyAction, CutAction, DeleteAction, NewAction, PasteAction, RenameAction, + ) # DATA CLASSES - class Employee ( HasTraits ): - name = Str( '' ) + class Employee(HasTraits): + name = Str('') title = Str() - phone = Regex( regex = r'\d\d\d-\d\d\d\d' ) + phone = Regex(regex=r'\d\d\d-\d\d\d\d') - def default_title ( self ): + def default_title(self): self.title = 'Senior Engineer' - class Department ( HasTraits ): - name = Str( '' ) - employees = List( Employee ) + class Department(HasTraits): + name = Str('') + employees = List(Employee) - class Company ( HasTraits ): - name = Str( '' ) - departments = List( Department ) - employees = List( Employee ) + class Company(HasTraits): + name = Str('') + departments = List(Department) + employees = List(Employee) - class Owner ( HasTraits ): - name = Str( '' ) - company = Instance( Company ) + class Owner(HasTraits): + name = Str('') + company = Instance(Company) # INSTANCES jason = Employee( - name = 'Jason', - title = 'Engineer', - phone = '536-1057' ) + name='Jason', + title='Engineer', + phone='536-1057', + ) mike = Employee( - name = 'Mike', - title = 'Sr. Marketing Analyst', - phone = '536-1057' ) + name='Mike', + title='Sr. Marketing Analyst', + phone='536-1057', + ) dave = Employee( - name = 'Dave', - title = 'Sr. Engineer', - phone = '536-1057' ) + name='Dave', + title='Sr. Engineer', + phone='536-1057', + ) susan = Employee( - name = 'Susan', - title = 'Engineer', - phone = '536-1057' ) + name='Susan', + title='Engineer', + phone='536-1057', + ) betty = Employee( - name = 'Betty', - title = 'Marketing Analyst' ) + name='Betty', + title='Marketing Analyst', + ) owner = Owner( - name = 'wile', - company = Company( - name = 'Acme Labs, Inc.', - departments = [ + name='wile', + company=Company( + name='Acme Labs, Inc.', + departments=[ Department( - name = 'Marketing', - employees = [ mike, betty ] + name='Marketing', + employees=[mike, betty], ), Department( - name = 'Engineering', - employees = [ dave, susan, jason ] + name='Engineering', + employees=[dave, susan, jason], ) ], - employees = [ dave, susan, mike, betty, 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') + def_title_action = Action( + name='Default title', action='object.default', + ) dept_action = Action( name='Department', - action='handler.employee_department(editor,object)') + 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' ) + HGroup('3', 'name'), + HGroup('9', 'title'), + HGroup('phone'), + id='vsplit', + ), + id='traits.doc.example.treeeditor', + dock='vertical', + ) - class TreeHandler ( Handler ): + 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 ) + def employee_department(self, editor, object): + dept = editor.get_parent(object) + print(f'{object.name} works in the {dept.name} department.') # 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 ) - ] + 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 ) + 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 ) + owner.configure_traits(view=view) .. _defining-nodes: @@ -963,13 +993,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 +1021,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 +1069,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 +1223,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..8278822ed 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -68,31 +68,38 @@ The following code generates the editors shown in Figure 21. # 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 traits.api import Array, HasPrivateTraits + from traitsui.api import ArrayEditor, Item, View from traitsui.menu import NoButtons - class ArrayEditorTest ( HasPrivateTraits ): + 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 ) + + 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__': @@ -549,23 +556,28 @@ tags, and then strips out the tags. :: # enum_editor.py -- Example of using an enumeration editor - from traits.api import HasTraits, Enum + from traits.api import Enum, HasTraits 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', }))) + 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() @@ -907,32 +919,27 @@ Example 16: Instance editor with instance selection :: # instance_editor_selection.py -- Example of an instance editor - # with instance selection + # with instance selection - from traits.api \ - import HasStrictTraits, Int, Instance, List, Regex, Str - from traitsui.api \ - import View, Item, InstanceEditor + 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 = '\d\d\d[-]\d\d\d\d', - ) + 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='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') + Person(name='Fields', age=31, phone='555-3547'), ] class Team(HasStrictTraits): @@ -947,19 +954,19 @@ Example 16: Instance editor with instance selection Item( 'captain', label='Team Captain', - editor = InstanceEditor( - name = 'roster', - editable = True), - style = 'custom', + editor=InstanceEditor( + name='roster', + editable=True), + style='custom', ), - buttons = ['OK'] + buttons=['OK'], ) if __name__ == '__main__': team = Team( - name = 'Vultures', - captain = people[0], - roster = people + name='Vultures', + captain=people[0], + roster=people, ) team.configure_traits() diff --git a/docs/source/traitsui_user_manual/factory_intro.rst b/docs/source/traitsui_user_manual/factory_intro.rst index 5a561f034..e4810ab04 100644 --- a/docs/source/traitsui_user_manual/factory_intro.rst +++ b/docs/source/traitsui_user_manual/factory_intro.rst @@ -36,27 +36,30 @@ Consider the following script and the window it creates: :: # default_trait_editors.py -- Example of using default - # trait editors + # trait editors - from traits.api import HasTraits, Str, Range, Bool - from traitsui.api import View, Item + 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) + 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'), + ) - 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 = Adult( + first_name='Alice', + last_name='Smith', + age=42, + registered_voter=True, + ) alice.configure_traits() @@ -180,7 +183,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 @@ -332,28 +340,31 @@ example, consider the following script: :: # mixed_styles.py -- Example of using editor styles at - # various levels + # various levels - from traits.api import HasTraits, Str, Enum - from traitsui.api import View, Group, Item + 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') + 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/handler.rst b/docs/source/traitsui_user_manual/handler.rst index 7dc22c572..d48d5c392 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 @@ -445,11 +445,10 @@ overridden setattr() method and user interface notification method. :: # handler_override.py -- Example of a Handler that overrides - # setattr(), and that has a user interface - # notification method + # setattr(), and that has a user interface notification method - from traits.api import HasTraits, Bool - from traitsui.api import View, Handler + from traits.api import Bool, HasTraits + from traitsui.api import Handler, View class TC_Handler(Handler): @@ -467,10 +466,14 @@ overridden setattr() method and user interface notification method. b3 = Bool() _updated = Bool(False) - view1 = View('b1', 'b2', 'b3', - title="Alter Title", - handler=TC_Handler(), - buttons = ['OK', 'Cancel']) + view1 = View( + 'b1', + 'b2', + 'b3', + title="Alter Title", + handler=TC_Handler(), + buttons=['OK', 'Cancel'], + ) tc = TestClass() tc.configure_traits(view=view1) @@ -508,8 +511,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 +525,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 +549,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 +574,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 +588,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..3d0424ce4 100644 --- a/docs/source/traitsui_user_manual/view.rst +++ b/docs/source/traitsui_user_manual/view.rst @@ -22,8 +22,8 @@ object: :: # configure_traits.py -- Sample code to demonstrate - # configure_traits() - from traits.api import HasTraits, Str, Int + # configure_traits() + from traits.api import HasTraits, Int, Str import traitsui class SimpleEmployee(HasTraits): @@ -75,10 +75,10 @@ View object and passing it to the configure_traits() method: :: # configure_traits_view.py -- Sample code to demonstrate - # configure_traits() + # configure_traits() - from traits.api import HasTraits, Str, Int - from traitsui.api import View, Item + from traits.api import HasTraits, Int, Str + from traitsui.api import Item, View import traitsui class SimpleEmployee(HasTraits): @@ -88,9 +88,11 @@ View object and passing it to the configure_traits() method: employee_number = Str() salary = Int() - view1 = View(Item(name = 'first_name'), - Item(name = 'last_name'), - Item(name = 'department')) + view1 = View( + Item(name='first_name'), + Item(name='last_name'), + Item(name='department'), + ) sam = SimpleEmployee() sam.configure_traits(view=view1) @@ -350,9 +352,9 @@ Consider the following enhancement to Example 2: :: # 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 + # configure_traits() + from traits.api import HasTraits, Int, Str + from traitsui.api import Group, Item, View import traitsui class SimpleEmployee(HasTraits): @@ -363,11 +365,15 @@ Consider the following enhancement to Example 2: 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)) + 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) From a070f6496f4f05f7591f27c7b349f551cf001831 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:35:06 +0530 Subject: [PATCH 02/18] CLN : Move docs examples into the docs tree and literalinclude the example files in the documentation rather than manually duplicating the information in the docs again this commit only moves examples and removes duplicates from factory intro new file: docs/source/traitsui_user_manual/examples/default_trait_editors.py new file: docs/source/traitsui_user_manual/examples/mixed_styles.py modified: docs/source/traitsui_user_manual/factory_intro.rst deleted: examples/tutorials/doc_examples/examples/default_trait_editors.py deleted: examples/tutorials/doc_examples/examples/mixed_styles.py --- .../examples/default_trait_editors.py | 38 +++++++++++ .../examples/mixed_styles.py | 40 ++++++++++++ .../traitsui_user_manual/factory_intro.rst | 64 ++----------------- .../examples/default_trait_editors.py | 30 --------- .../doc_examples/examples/mixed_styles.py | 33 ---------- 5 files changed, 82 insertions(+), 123 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/default_trait_editors.py create mode 100644 docs/source/traitsui_user_manual/examples/mixed_styles.py delete mode 100644 examples/tutorials/doc_examples/examples/default_trait_editors.py delete mode 100644 examples/tutorials/doc_examples/examples/mixed_styles.py 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/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/factory_intro.rst b/docs/source/traitsui_user_manual/factory_intro.rst index e4810ab04..dc9d802be 100644 --- a/docs/source/traitsui_user_manual/factory_intro.rst +++ b/docs/source/traitsui_user_manual/factory_intro.rst @@ -33,35 +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 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() +.. 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 @@ -337,37 +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 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() +.. literalinclude:: examples/mixed_styles.py + :start-at: mixed_styles.py Notice how the editor styles are set for each attribute: 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/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() From f374b49cfb4702e56e7af32822d60c122ca26be3 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:40:40 +0530 Subject: [PATCH 03/18] CLN : Replace array editor with literalinclude in factories basic new file: docs/source/traitsui_user_manual/examples/array_editor.py modified: docs/source/traitsui_user_manual/factories_basic.rst deleted: examples/tutorials/doc_examples/examples/array_editor.py --- .../examples/array_editor.py | 50 +++++++++++++++++++ .../traitsui_user_manual/factories_basic.rst | 43 +--------------- .../doc_examples/examples/array_editor.py | 39 --------------- 3 files changed, 52 insertions(+), 80 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/array_editor.py delete mode 100644 examples/tutorials/doc_examples/examples/array_editor.py 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..1927968f8 --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/array_editor.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! + +# 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/factories_basic.rst b/docs/source/traitsui_user_manual/factories_basic.rst index 8278822ed..3af298554 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -63,47 +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 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() +.. literalinclude:: examples/array_editor.py + :start-at: array_editor.py BooleanEditor() ``````````````` 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() From 2944737c517c88204e4786502f347f4b686c92f0 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:46:50 +0530 Subject: [PATCH 04/18] CLN : literalinclude enum editor in factories basic new file: docs/source/traitsui_user_manual/examples/enum_editor.py modified: docs/source/traitsui_user_manual/factories_basic.rst deleted: examples/tutorials/doc_examples/examples/enum_editor.py --- .../examples/enum_editor.py | 34 +++++++++++++++++++ .../traitsui_user_manual/factories_basic.rst | 29 ++-------------- .../doc_examples/examples/enum_editor.py | 29 ---------------- 3 files changed, 36 insertions(+), 56 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/enum_editor.py delete mode 100644 examples/tutorials/doc_examples/examples/enum_editor.py 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..57a0a174d --- /dev/null +++ b/docs/source/traitsui_user_manual/examples/enum_editor.py @@ -0,0 +1,34 @@ +# (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', '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/docs/source/traitsui_user_manual/factories_basic.rst b/docs/source/traitsui_user_manual/factories_basic.rst index 3af298554..f7eb7ac39 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -514,33 +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 Enum, HasTraits - from traitsui.api import EnumEditor - - 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() +.. 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 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() From aefa4acc3764f81a189b1d8d1b60a2361254cfcd Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:47:17 +0530 Subject: [PATCH 05/18] CLN : code formatting in array editor example modified: docs/source/traitsui_user_manual/examples/array_editor.py --- .../traitsui_user_manual/examples/array_editor.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/docs/source/traitsui_user_manual/examples/array_editor.py b/docs/source/traitsui_user_manual/examples/array_editor.py index 1927968f8..0431f0888 100644 --- a/docs/source/traitsui_user_manual/examples/array_editor.py +++ b/docs/source/traitsui_user_manual/examples/array_editor.py @@ -28,19 +28,11 @@ class ArrayEditorTest(HasPrivateTraits): view = View( Item('three', label='3x3 Integer'), '_', - Item( - 'three', - label='Integer Read-only', - style='readonly', - ), + Item('three', label='Integer Read-only', style='readonly'), '_', Item('four', label='4x4 Float'), '_', - Item( - 'four', - label='Float Read-only', - style='readonly', - ), + Item('four', label='Float Read-only', style='readonly'), buttons=NoButtons, resizable=True, ) From 0ea8f2ec12430cc0324880ff811c8b99fb9ba7e9 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:53:59 +0530 Subject: [PATCH 06/18] CLN : literalinclude instance editor selection new file: docs/source/traitsui_user_manual/examples/instance_editor_selection.py modified: docs/source/traitsui_user_manual/factories_basic.rst deleted: examples/tutorials/doc_examples/examples/instance_editor_selection.py --- .../examples/instance_editor_selection.py | 59 ++++++++++++++++++ .../traitsui_user_manual/factories_basic.rst | 55 +---------------- .../examples/instance_editor_selection.py | 60 ------------------- 3 files changed, 61 insertions(+), 113 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/instance_editor_selection.py delete mode 100644 examples/tutorials/doc_examples/examples/instance_editor_selection.py 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/factories_basic.rst b/docs/source/traitsui_user_manual/factories_basic.rst index f7eb7ac39..3b71d4173 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -852,59 +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, 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='\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/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() From 5ac7176c584c4163e529f6b2d21ccc21154af2d0 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 16:59:05 +0530 Subject: [PATCH 07/18] CLN : literalinclude key bindings example new file: docs/source/traitsui_user_manual/examples/key_bindings.py modified: docs/source/traitsui_user_manual/factories_advanced_extra.rst deleted: examples/tutorials/doc_examples/examples/key_bindings.py --- .../examples/key_bindings.py | 76 +++++++++++++++++++ .../factories_advanced_extra.rst | 68 +---------------- .../doc_examples/examples/key_bindings.py | 73 ------------------ 3 files changed, 78 insertions(+), 139 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/key_bindings.py delete mode 100644 examples/tutorials/doc_examples/examples/key_bindings.py 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/factories_advanced_extra.rst b/docs/source/traitsui_user_manual/factories_advanced_extra.rst index 1749125ff..01f6ab544 100644 --- a/docs/source/traitsui_user_manual/factories_advanced_extra.rst +++ b/docs/source/traitsui_user_manual/factories_advanced_extra.rst @@ -128,72 +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 CodeEditor, 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', - ), - ) - - # 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: 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() From 51c091bf9fbca00c2c7aec1bf3dcd75231ca9f7e Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 17:14:28 +0530 Subject: [PATCH 08/18] CLN : literalinclude tree editor example new file: docs/source/traitsui_user_manual/examples/tree_editor.py modified: docs/source/traitsui_user_manual/factories_advanced_extra.rst deleted: examples/tutorials/doc_examples/examples/tree_editor.py --- .../examples/tree_editor.py | 184 ++++++++++++++ .../factories_advanced_extra.rst | 225 +----------------- .../doc_examples/examples/tree_editor.py | 206 ---------------- 3 files changed, 186 insertions(+), 429 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/tree_editor.py delete mode 100644 examples/tutorials/doc_examples/examples/tree_editor.py 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/factories_advanced_extra.rst b/docs/source/traitsui_user_manual/factories_advanced_extra.rst index 01f6ab544..d22874be2 100644 --- a/docs/source/traitsui_user_manual/factories_advanced_extra.rst +++ b/docs/source/traitsui_user_manual/factories_advanced_extra.rst @@ -679,229 +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, Instance, List, Str, Regex - from traitsui.api import ( - Group, Handler, HGroup, Item, TreeEditor, TreeNode, View, VSplit, - ) - from traitsui.menu import Action, Menu, Separator - from traitsui.wx.tree_editor import ( - CopyAction, CutAction, DeleteAction, NewAction, PasteAction, 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(f'{object.name} works in the {dept.name} department.') - - # 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: 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) From 37d22915efc81fa4b7a89a56481824d144cf73f5 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 17:22:14 +0530 Subject: [PATCH 09/18] CLN : literalinclude wizard and configure_traits_view_buttons modified: docs/source/traitsui_user_manual/custom_view.rst new file: docs/source/traitsui_user_manual/examples/wizard.py modified: examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py deleted: examples/tutorials/doc_examples/examples/wizard.py --- .../traitsui_user_manual/custom_view.rst | 63 ++----------------- .../traitsui_user_manual/examples/wizard.py | 41 ++++++++++++ .../examples/configure_traits_view_buttons.py | 30 +++++---- .../tutorials/doc_examples/examples/wizard.py | 39 ------------ 4 files changed, 62 insertions(+), 111 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/wizard.py delete mode 100644 examples/tutorials/doc_examples/examples/wizard.py diff --git a/docs/source/traitsui_user_manual/custom_view.rst b/docs/source/traitsui_user_manual/custom_view.rst index ab7b15e1b..c04931b1a 100644 --- a/docs/source/traitsui_user_manual/custom_view.rst +++ b/docs/source/traitsui_user_manual/custom_view.rst @@ -101,39 +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: @@ -198,32 +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`: 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/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py b/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py index 9494c60a0..7cc227578 100644 --- a/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py +++ b/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py @@ -1,16 +1,18 @@ -# Copyright (c) 2007, Enthought, Inc. -# License: BSD Style. - +# (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() +# configure_traits() -# --[Imports]-------------------------------------------------------------- from traits.api import HasTraits, Str, Int -from traitsui.api import View, Item -from traitsui.menu import OKButton, CancelButton - -# --[Code]----------------------------------------------------------------- +from traitsui.api import CancelButton, Item, OKButton, View class SimpleEmployee(HasTraits): @@ -22,10 +24,12 @@ class SimpleEmployee(HasTraits): salary = Int() -view1 = View(Item(name='first_name'), - Item(name='last_name'), - Item(name='department'), - buttons=[OKButton, CancelButton]) +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/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') From 2d1208a2953bebdfdba1ba0e7a4e4b4b8b8586a5 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 21:39:06 +0530 Subject: [PATCH 10/18] CLN : literalinclude default_traits_view and multiple_views modified: docs/source/traitsui_user_manual/advanced_view.rst new file: docs/source/traitsui_user_manual/examples/default_traits_view.py new file: docs/source/traitsui_user_manual/examples/multiple_views.py deleted: examples/tutorials/doc_examples/examples/default_traits_view.py deleted: examples/tutorials/doc_examples/examples/multiple_views.py --- .../traitsui_user_manual/advanced_view.rst | 73 +------------------ .../examples/default_traits_view.py | 37 ++++++++++ .../examples/multiple_views.py | 50 +++++++++++++ .../examples/default_traits_view.py | 29 -------- .../doc_examples/examples/multiple_views.py | 39 ---------- 5 files changed, 91 insertions(+), 137 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/default_traits_view.py create mode 100644 docs/source/traitsui_user_manual/examples/multiple_views.py delete mode 100644 examples/tutorials/doc_examples/examples/default_traits_view.py delete mode 100644 examples/tutorials/doc_examples/examples/multiple_views.py diff --git a/docs/source/traitsui_user_manual/advanced_view.rst b/docs/source/traitsui_user_manual/advanced_view.rst index 4446763b1..0319b2ba0 100644 --- a/docs/source/traitsui_user_manual/advanced_view.rst +++ b/docs/source/traitsui_user_manual/advanced_view.rst @@ -63,34 +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 @@ -173,47 +147,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 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/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/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/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') From d37c3292389d83e2f673995547b52afda8c35b2a Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 21:44:24 +0530 Subject: [PATCH 11/18] CLN : literalinclude multi_object_view modified: docs/source/traitsui_user_manual/advanced_view.rst new file: docs/source/traitsui_user_manual/examples/multi_object_view.py deleted: examples/tutorials/doc_examples/examples/multi_object_view.py --- .../traitsui_user_manual/advanced_view.rst | 56 +---------------- .../examples/multi_object_view.py | 61 +++++++++++++++++++ .../examples/multi_object_view.py | 55 ----------------- 3 files changed, 63 insertions(+), 109 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/multi_object_view.py delete mode 100644 examples/tutorials/doc_examples/examples/multi_object_view.py diff --git a/docs/source/traitsui_user_manual/advanced_view.rst b/docs/source/traitsui_user_manual/advanced_view.rst index 0319b2ba0..cc3bfbf50 100644 --- a/docs/source/traitsui_user_manual/advanced_view.rst +++ b/docs/source/traitsui_user_manual/advanced_view.rst @@ -343,60 +343,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? 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/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}) From 7c49ae565869664469b43f14c35ec11f938be2b1 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 21:50:17 +0530 Subject: [PATCH 12/18] CLN : literalinclude configure_traits_view_group new file: docs/source/traitsui_user_manual/examples/configure_traits_view_group.py modified: docs/source/traitsui_user_manual/view.rst deleted: examples/tutorials/doc_examples/examples/configure_traits_view_group.py --- .../examples/configure_traits_view_group.py | 38 +++++++++++++++++++ docs/source/traitsui_user_manual/view.rst | 30 +-------------- .../examples/configure_traits_view_group.py | 32 ---------------- 3 files changed, 40 insertions(+), 60 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/configure_traits_view_group.py delete mode 100644 examples/tutorials/doc_examples/examples/configure_traits_view_group.py 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/view.rst b/docs/source/traitsui_user_manual/view.rst index 3d0424ce4..20a2689ac 100644 --- a/docs/source/traitsui_user_manual/view.rst +++ b/docs/source/traitsui_user_manual/view.rst @@ -349,34 +349,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, Int, Str - from traitsui.api import Group, Item, View - 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/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) From d3094e5feab608f3067620766226db5401bb0559 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 21:54:10 +0530 Subject: [PATCH 13/18] CLN : literalinclude configure_traits_view new file: docs/source/traitsui_user_manual/examples/configure_traits_view.py modified: docs/source/traitsui_user_manual/view.rst deleted: examples/tutorials/doc_examples/examples/configure_traits_view.py --- .../examples/configure_traits_view.py | 32 +++++++++++++++++++ docs/source/traitsui_user_manual/view.rst | 26 ++------------- .../examples/configure_traits_view.py | 28 ---------------- 3 files changed, 34 insertions(+), 52 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/configure_traits_view.py delete mode 100644 examples/tutorials/doc_examples/examples/configure_traits_view.py 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/view.rst b/docs/source/traitsui_user_manual/view.rst index 20a2689ac..36e85024b 100644 --- a/docs/source/traitsui_user_manual/view.rst +++ b/docs/source/traitsui_user_manual/view.rst @@ -72,30 +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, Int, Str - from traitsui.api import Item, View - 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: 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) From 82c895effb5a4112a1e544053bd7afdc0f0a75f2 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 22:00:42 +0530 Subject: [PATCH 14/18] CLN : literalinclude handler_override Also move the configure_traits_view_buttons file - which i missed before renamed: examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py -> docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py new file: docs/source/traitsui_user_manual/examples/handler_override.py modified: docs/source/traitsui_user_manual/handler.rst deleted: examples/tutorials/doc_examples/examples/handler_override.py --- .../examples/configure_traits_view_buttons.py | 0 .../examples/handler_override.py | 46 +++++++++++++++++++ docs/source/traitsui_user_manual/handler.rst | 37 +-------------- .../doc_examples/examples/handler_override.py | 39 ---------------- 4 files changed, 48 insertions(+), 74 deletions(-) rename {examples/tutorials/doc_examples => docs/source/traitsui_user_manual}/examples/configure_traits_view_buttons.py (100%) create mode 100644 docs/source/traitsui_user_manual/examples/handler_override.py delete mode 100644 examples/tutorials/doc_examples/examples/handler_override.py diff --git a/examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py b/docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py similarity index 100% rename from examples/tutorials/doc_examples/examples/configure_traits_view_buttons.py rename to docs/source/traitsui_user_manual/examples/configure_traits_view_buttons.py 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/handler.rst b/docs/source/traitsui_user_manual/handler.rst index d48d5c392..18bd798f1 100644 --- a/docs/source/traitsui_user_manual/handler.rst +++ b/docs/source/traitsui_user_manual/handler.rst @@ -442,41 +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 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) +.. 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" 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) From 670d09b2180aa20baf1fbf80e0d1d41125c0756e Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Mon, 3 May 2021 22:12:08 +0530 Subject: [PATCH 15/18] FIX : Ignore wizard in integrationtests modified: integrationtests/test_all_examples.py --- integrationtests/test_all_examples.py | 4 ---- 1 file changed, 4 deletions(-) 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() From b37b99ed55f6771efe0eada466b0771faed230a2 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Tue, 4 May 2021 15:02:08 +0530 Subject: [PATCH 16/18] CLN : Minor formatting issues modified: docs/source/traitsui_user_manual/custom_view.rst modified: docs/source/traitsui_user_manual/factories_basic.rst modified: docs/source/traitsui_user_manual/view.rst --- docs/source/traitsui_user_manual/custom_view.rst | 6 +++--- docs/source/traitsui_user_manual/factories_basic.rst | 2 +- docs/source/traitsui_user_manual/view.rst | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/source/traitsui_user_manual/custom_view.rst b/docs/source/traitsui_user_manual/custom_view.rst index c04931b1a..0da9c4139 100644 --- a/docs/source/traitsui_user_manual/custom_view.rst +++ b/docs/source/traitsui_user_manual/custom_view.rst @@ -208,9 +208,9 @@ 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:: diff --git a/docs/source/traitsui_user_manual/factories_basic.rst b/docs/source/traitsui_user_manual/factories_basic.rst index 3b71d4173..0e637c67f 100644 --- a/docs/source/traitsui_user_manual/factories_basic.rst +++ b/docs/source/traitsui_user_manual/factories_basic.rst @@ -98,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. diff --git a/docs/source/traitsui_user_manual/view.rst b/docs/source/traitsui_user_manual/view.rst index 36e85024b..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() + # configure_traits.py -- Sample code to demonstrate configure_traits() from traits.api import HasTraits, Int, Str - import traitsui + class SimpleEmployee(HasTraits): first_name = Str() @@ -34,6 +33,7 @@ object: employee_number = Str() salary = Int() + sam = SimpleEmployee() sam.configure_traits() From e37c5bcb595c8c22c73ca463a9f56b08c7e7d9da Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Wed, 5 May 2021 15:44:24 +0530 Subject: [PATCH 17/18] CLN : literalinclude default_traits_view2 after creating it first modified: docs/source/traitsui_user_manual/advanced_view.rst new file: docs/source/traitsui_user_manual/examples/default_traits_view2.py --- .../traitsui_user_manual/advanced_view.rst | 31 +-------------- .../examples/default_traits_view2.py | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 docs/source/traitsui_user_manual/examples/default_traits_view2.py diff --git a/docs/source/traitsui_user_manual/advanced_view.rst b/docs/source/traitsui_user_manual/advanced_view.rst index cc3bfbf50..05607fffb 100644 --- a/docs/source/traitsui_user_manual/advanced_view.rst +++ b/docs/source/traitsui_user_manual/advanced_view.rst @@ -93,35 +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 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() From 393695ccd795beeeedd4fecad2bd2b08d70ef8fa Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Wed, 5 May 2021 15:48:41 +0530 Subject: [PATCH 18/18] CLN : minor formatting changes based on PR review modified: docs/source/traitsui_user_manual/examples/enum_editor.py --- .../examples/enum_editor.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/source/traitsui_user_manual/examples/enum_editor.py b/docs/source/traitsui_user_manual/examples/enum_editor.py index 57a0a174d..ddd14b9ce 100644 --- a/docs/source/traitsui_user_manual/examples/enum_editor.py +++ b/docs/source/traitsui_user_manual/examples/enum_editor.py @@ -15,18 +15,20 @@ class EnumExample(HasTraits): - priority = Enum('Medium', 'Highest', 'High', 'Medium', 'Low', 'Lowest') + 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', - }), + editor=EnumEditor( + values={ + 'Highest': '1:Highest', + 'High': '2:High', + 'Medium': '3:Medium', + 'Low': '4:Low', + 'Lowest': '5:Lowest', + } + ), ), )