Search code examples
pythonlistscrollabletraitsui

Scrollable Group with a List using TraitsUI


So I have a problem with my traitsUI code. What I want to have is a List that is shown using CheckListEditor() and is in a confined space so it could be scrollable. I need to have this List in a Group because this would be only a small part of the whole GUI. Yet if I set Group property "scrollable" to "True" that does nothing. Any ideas? Here is minimal working example:

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()

Solution

  • It's the View that should be made scrollable:

    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()
    

    I get this: enter image description here

    If you want to embed multiple scrollable views you can do it with Instances and 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()
    

    ...with this result:

    enter image description here