enthought / enthought/traitsui
TreeEditor Menu problem
- Dominant language
- Python
- Stars
- 306
- Forks
- 99
- PR merge metrics
- No merged PRs in 30d
Description
I'm referring to the bugfix #1744, this fix works for me too.
I've found another bug regarding using custom menus of TreeEditor.
```
from traits.api import HasTraits, Instance, List, Str
from traitsui.api import (
Menu,
UItem,
TreeEditor,
TreeNode,
View,
)
from traitsui.editors.tree_editor import NewAction
class Folder(HasTraits):
name = Str('Folder')
children = List()
class SingleItem(HasTraits):
name = Str('Item')
tree_editor = TreeEditor(
nodes=[
TreeNode(
node_for=[Folder],
children='children',
label='name',
add=[Folder, SingleItem],
menu=Menu(NewAction),
auto_open=True,
view=View(),
),
TreeNode(
node_for=[SingleItem],
label='name',
view=View(),
auto_open=True,
),
]
)
class A(HasTraits):
container = Instance(Folder, ())
myview = View(
UItem(name='container', editor=tree_editor),
)
a = A()
# Add an item via context menu
a.configure_traits(view=myview)
# Add another item via context menu -> will crash
a.configure_traits(view=myview)
```
For seeing the bug, apply the bugfix of #1744, then:
- on first configure_traits add a new item using the context menu
- close the widget
- on second configure_traits add another item using the context menu -> this will crash:
```
File "C:\git\teehouse\.tox\py38-dev\lib\site-packages\traitsui\qt4\tree_editor.py", line 1229, in _perform
action.on_perform(object)
File "C:\git\teehouse\.tox\py38-dev\lib\site-packages\traitsui\qt4\tree_editor.py", line 1080, in perform_add
self._menu_new_node(factory, prompt)
File "C:\git\teehouse\.tox\py38-dev\lib\site-packages\traitsui\qt4\tree_editor.py", line 1326, in _menu_new_node
node, object, nid = self._data
TypeError: cannot unpack non-iterable NoneType object
```
Everything work when NOT defining a custom menu.
So what is the difference? Without defining a menu, a standard menu will be created each time configure_traits is called -> no problems.
If a menu is predefined in the editor, this menu is existing only once and creating the "new" actions will be skipped the second time configure_traits is called.
A workaround that is working for me in the meanwhile is using two separate instances of view and tree editor:
```
from traits.api import HasTraits, Instance, List, Str
from traitsui.api import (
Menu,
UItem,
TreeEditor,
TreeNode,
View,
)
from traitsui.editors.tree_editor import NewAction
class Folder(HasTraits):
name = Str('Folder')
children = List()
class SingleItem(HasTraits):
name = Str('Item')
tree_editor = lambda: TreeEditor(
nodes=[
TreeNode(
node_for=[Folder],
children='children',
label='name',
add=[Folder, SingleItem],
menu=Menu(NewAction),
auto_open=True,
view=View(),
),
TreeNode(
node_for=[SingleItem],
label='name',
view=View(),
auto_open=True,
),
]
)
class A(HasTraits):
container = Instance(Folder, ())
myview = lambda: View(
UItem(name='container', editor=tree_editor()),
)
a = A()
# Add an item via context menu
a.configure_traits(view=myview())
# Add another item via context menu -> will crash
a.configure_traits(view=myview())
```
traitsui version 7.2.1 with #1744 applied, Win10
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.