如何調整ListEditor以使用TraitsUI列出任意集合的內容?下面是一個示例代碼使用TraitsUI ListEditor Factory進行任意集合
from traits.api import HasStrictTraits, Instance, Int, List, Str
from traitsui.api import View, Item, ListEditor, InstanceEditor
from sortedcontainers import SortedListWithKey
class Person(HasStrictTraits):
name = Str
age = Int
class Office(HasStrictTraits):
# employees = Instance(SortedListWithKey,
kw={'key': lambda employee: employee.age})
employees = List
employee_view = View(
Item(name='name', show_label=False, style='readonly')
)
office_view = View(
Item(name='adults',
show_label=False,
style='readonly',
editor=ListEditor(
style='custom',
editor=InstanceEditor(view=employee_view),
),
),
resizable=True
)
employee_list = [Person(name='John', age=31), Person(name='Mike', age=31),
Person(name='Jill', age=37), Person(name='Eric', age=28)]
#office = Office()
#office.employees.update(employee_list)
office = Office(employees=employee_list)
office.configure_traits(view=office_view)
如果我用我註釋掉的代碼替換SortedListWithKey標準的名單,我得到「AttributeError的:‘辦公室’對象有沒有屬性‘值’」的錯誤。我該如何解決這個問題?
澄清:在我看來,排序不僅僅是一種觀點,而是「業務邏輯」的一部分。你說得對,'SortedListWithKey'來自「Sorted Containers」包。 – MindV0rtex