В соответствии с предложениями, приведенными здесь: https://docs.enthought.com/traitsui/traitsui_user_manual/factories_basic.html#enumeditor, Я хочу использовать EnumEditor
для сопоставленной черты, установив для параметра values указатель на сопоставленную черту:
Параметр значений может быть списком, кортежем или словарем или «отображенной» чертой.
Это работает:
from traits.api import (HasTraits, Trait)
from traitsui.api import (EnumEditor, View, Item)
class foo(HasTraits):
bar = Trait("One", {"One": 1, "Two": 2})
view = View(Item(name="bar", label="Bar", editor=EnumEditor(values=bar)))
foo().configure_traits()
Но в моемФактический вариант использования, я определяю представление вне класса foo
:
from traits.api import (HasTraits, Trait)
from traitsui.api import (EnumEditor, View, Item)
class foo(HasTraits):
bar = Trait("One", {"One": 1, "Two": 2})
theView = View(Item(name="bar", label="Bar", editor=EnumEditor(values=bar)))
foo().configure_traits(view=theView)
, что дает:
Traceback (most recent call last):
File "tst_enum_mapped2.py", line 7, in <module>
theView = View(Item(name="bar", label="Bar", editor=EnumEditor(values=bar)))
NameError: name 'bar' is not defined
Я пробовал это:
theView = View(Item(name="bar", label="Bar", editor=EnumEditor(values=foo.bar)))
и получил это:
Traceback (most recent call last):
File "tst_enum_mapped2.py", line 7, in <module>
theView = View(Item(name="bar", label="Bar", editor=EnumEditor(values=foo.bar)))
AttributeError: type object 'foo' has no attribute 'bar'
И я попробовал это:
theView = View(Item(name="bar", label="Bar", editor=EnumEditor(values=foo().bar)))
и получил это:
Exception occurred in traits notification handler.
Please check the log file for details.
Exception occurred in traits notification handler for object: <traitsui.editors.enum_editor.ToolkitEditorFactory object at 0x000000000608EA68>, trait: values, old value: None, new value: One
Traceback (most recent call last):
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traits\trait_notifiers.py", line 394, in __call__
self.handler(*args)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\editor_factory.py", line 324, in _values_changed
enum_values_changed(self.values, strfunc=self.string_value)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\helper.py", line 103, in enum_values_changed
raise TraitError("Invalid value for 'values' specified")
traits.trait_errors.TraitError: Invalid value for 'values' specified
Traceback (most recent call last):
File "tst_enum_mapped2.py", line 9, in <module>
foo().configure_traits(view=theView)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traits\has_traits.py", line 2352, in configure_traits
args,
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\toolkit.py", line 224, in view_application
id, scrollable, args)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\view_application.py", line 85, in view_application
scrollable, args).ui.result
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\view_application.py", line 128, in __init__
args=self.args)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\view.py", line 449, in ui
ui.ui(parent, kind)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\ui.py", line 237, in ui
self.rebuild(self, parent)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\toolkit.py", line 166, in ui_live
ui_live.ui_live(ui, parent)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_live.py", line 41, in ui_live
_ui_dialog(ui, parent, BaseDialog.NONMODAL)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_live.py", line 63, in _ui_dialog
BaseDialog.display_ui(ui, parent, style)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_base.py", line 278, in display_ui
ui.owner.init(ui, parent, style)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_live.py", line 203, in init
self.add_contents(panel(ui), bbox)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_panel.py", line 265, in panel
panel = _GroupPanel(content[0], ui).control
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_panel.py", line 605, in __init__
layout = self._add_items(content, inner)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\ui_panel.py", line 876, in _add_items
editor.prepare(inner)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\editor.py", line 170, in prepare
self.init(parent)
File "C:\Users\dbanas\Anaconda3\envs\pybert-dev\lib\site-packages\traitsui\qt4\enum_editor.py", line 161, in init
control.addItems(self.names)
TypeError: addItems(self, Iterable[str]): argument 1 has unexpected type 'NoneType'
Есть ли работающая альтернативаопределить представление как члена класса?