Question

I am using CheckListEditor to let user choose a subset of available options. With the style set to 'custom', it displays a list of check-list boxes with labels alongside. This is what I wanted. But, one problem is that, the text content is displayed with case changed in some cases - I am confused why this happens.

For eg, if the text is ".state.Last", it gets displayed as ".state.last" .

Anyone know why this happens and if there is any workaround to this problem.

Thanks.

Was it helpful?

Solution

By default, the editor calls the string method capitalize on the text. I don't know why; perhaps the author thought this would help enforce a consistent style in the UI.

You can override this behavior with the format_func argument of the CheckListEditor. Here's an example. (I also used the label argument of the Item to override the capitalization of the editor's label.)

from traits.api import HasTraits, List, on_trait_change
from traitsui.api import Item, View, CheckListEditor


class Foo(HasTraits):
    stuff = List()

    traits_view = View(Item('stuff', style='custom', label='stuff',
        editor=CheckListEditor(values=['.state.First', '.state.Last', '.state.Any'],
                               format_func=lambda x: x)))

    @on_trait_change('stuff[]')
    def show_stuff(self):
        print "stuff =", self.stuff


if __name__ == "__main__":
    f = Foo()
    f.configure_traits()

Alternatively, you can give the values as a list of tuples. Each tuple has the form (obj, label), where label is the string that is displayed in the UI and obj is the object added to the list. When this form is used, the label is left unchanged in the UI. For example,

    traits_view = View(Item('stuff', style='custom', label='stuff',
        editor=CheckListEditor(values=[('.state.First',)*2,
                                       ('.state.Last',)*2,
                                       ('.state.Any',)*2])))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top