使用 TraitsUI 的带有列表的可滚动组
Scrollable Group with a List using TraitsUI
所以我的 traitsUI 代码有问题。我想要的是一个使用 CheckListEditor() 显示的列表,它位于一个受限的 space 中,因此它可以滚动。我需要将这个列表放在一个组中,因为这只是整个 GUI 的一小部分。然而,如果我将 Group 属性 "scrollable" 设置为 "True" 则什么都不做。有任何想法吗?
这是最小的工作示例:
from traits.api import HasTraits, List
from traitsui.api import View, ListEditor, Group, Item, CheckListEditor, Group
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name = 'full_list')),
scrollable = True,
orientation = 'vertical'),
height=100)
if __name__ == '__main__':
f = Foo()
f.configure_traits()
View
应该设为可滚动:
from traits.api import HasTraits, List
from traitsui.api import (
View, ListEditor, Group, Item, CheckListEditor, Group
)
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name='full_list')),
orientation='vertical'),
scrollable=True,
height=100)
if __name__ == '__main__':
f = Foo()
f.configure_traits()
我明白了:
如果您想嵌入多个可滚动视图,您可以使用 Instance
s 和 InstanceEditors
:
from traits.api import HasTraits, List, Instance
from traitsui.api import (
View, ListEditor, Group,InstanceEditor, Item, CheckListEditor, Group,
VGroup
)
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name='full_list')),
orientation='vertical'),
scrollable=True,
height=100)
class FooContainingView(HasTraits):
foo_1 = Instance(Foo)
foo_2 = Instance(Foo)
traits_view = View(
VGroup(
Item('foo_1',
editor=InstanceEditor(),
style='custom',
show_label=False,
),
Item('foo_2',
editor=InstanceEditor(),
style='custom',
show_label=False,
),
),
resizable=True,
)
if __name__ == '__main__':
f = Foo()
fcv = FooContainingView(foo_1=Foo(), foo_2=Foo())
fcv.configure_traits()
...结果如下:
所以我的 traitsUI 代码有问题。我想要的是一个使用 CheckListEditor() 显示的列表,它位于一个受限的 space 中,因此它可以滚动。我需要将这个列表放在一个组中,因为这只是整个 GUI 的一小部分。然而,如果我将 Group 属性 "scrollable" 设置为 "True" 则什么都不做。有任何想法吗? 这是最小的工作示例:
from traits.api import HasTraits, List
from traitsui.api import View, ListEditor, Group, Item, CheckListEditor, Group
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name = 'full_list')),
scrollable = True,
orientation = 'vertical'),
height=100)
if __name__ == '__main__':
f = Foo()
f.configure_traits()
View
应该设为可滚动:
from traits.api import HasTraits, List
from traitsui.api import (
View, ListEditor, Group, Item, CheckListEditor, Group
)
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name='full_list')),
orientation='vertical'),
scrollable=True,
height=100)
if __name__ == '__main__':
f = Foo()
f.configure_traits()
我明白了:
如果您想嵌入多个可滚动视图,您可以使用 Instance
s 和 InstanceEditors
:
from traits.api import HasTraits, List, Instance
from traitsui.api import (
View, ListEditor, Group,InstanceEditor, Item, CheckListEditor, Group,
VGroup
)
class Foo(HasTraits):
my_list = List()
full_list = List()
def _full_list_default(self):
return [str(n) for n in range(10)]
traits_view = View(Group(Item('my_list',
style='custom',
editor=CheckListEditor(name='full_list')),
orientation='vertical'),
scrollable=True,
height=100)
class FooContainingView(HasTraits):
foo_1 = Instance(Foo)
foo_2 = Instance(Foo)
traits_view = View(
VGroup(
Item('foo_1',
editor=InstanceEditor(),
style='custom',
show_label=False,
),
Item('foo_2',
editor=InstanceEditor(),
style='custom',
show_label=False,
),
),
resizable=True,
)
if __name__ == '__main__':
f = Foo()
fcv = FooContainingView(foo_1=Foo(), foo_2=Foo())
fcv.configure_traits()
...结果如下: