1

I am using a TreeEditor to represent model objects in my traitsui application. I would like the ability select multiple objects in the tree editor by holding down Shift or Ctrl and performing selections. My ultimate goal is to provide a quick way for the user to delete multiple objects in one go rather than right-clicking each item individually.

From reading the source code for tree editor I noticed,

selection_mode = Enum('single', 'extended')

which appears to define an extended selection mode. But I cannot find any example code which uses nor is it mentioned in the documentation of TreeEditor.

Is this supported by traitsui?

Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62

2 Answers2

1

FWIW, here's some relevant code snipped from a working application and sanitized. I have not tried to run it, so there could be some copy/paste/sanitize typos.

def default_traits_view(self):
    return View(
        UItem(
            'my_run_tree',
            editor=TreeEditor(
                nodes=[
                    TestRunTreeNode(
                        node_for=[Node0],
                        children='children',
                        label='label',
                    ),
                    TestRunTreeNode(
                        node_for=[Node1],
                        children='',
                        label='mylabel',
                    ),
                ],
                editable=False,
                selected='selected_nodes',
                selection_mode='extended',
            )
        ),
        resizable=True,
    )
Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • This was the clue I needed. Marked as answered. I'm going to paste in a modified version of the `tree_editor.py` example code from the traitsui documentation which demonstrates multiple selection. – Daniel Farrell Oct 24 '16 at 10:07
1

Here is an example demonstrating multiple selection. As Jonathan says,

  • Set selection_mode='extended'
  • Set selected attribute to a List(Any)

https://gist.github.com/danieljfarrell/24f838085172de9d20a4d3daa9f813b3

Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62