简体   繁体   English

TraitsUI CheckListEditor更改值的大小写

[英]TraitsUI CheckListEditor changing the case of values

I am using CheckListEditor to let user choose a subset of available options. 我正在使用CheckListEditor让用户选择可用选项的子集。 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" . 例如,如果文本是".state.Last" ,则它显示为".state.last"

Anyone know why this happens and if there is any workaround to this problem. 任何人都知道为什么会发生这种情况,以及是否有解决此问题的方法。

Thanks. 谢谢。

By default, the editor calls the string method capitalize on the text. 默认情况下,编辑器调用字符串方法来capitalize文本。 I don't know why; 我不知道为什么; perhaps the author thought this would help enforce a consistent style in the UI. 也许作者认为这有助于在UI中强制执行一致的风格。

You can override this behavior with the format_func argument of the CheckListEditor . 您可以覆盖与此行为format_func中的参数CheckListEditor Here's an example. 这是一个例子。 (I also used the label argument of the Item to override the capitalization of the editor's label.) (我还使用了Itemlabel参数来覆盖编辑器标签的大小写。)

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. 或者,您可以将values作为元组列表提供。 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. 每个元组都有表单(obj, label) ,其中label是UI中显示的字符串, obj是添加到列表中的对象。 When this form is used, the label is left unchanged in the UI. 使用此表单时,标签在UI中保持不变。 For example, 例如,

    traits_view = View(Item('stuff', style='custom', label='stuff',
        editor=CheckListEditor(values=[('.state.First',)*2,
                                       ('.state.Last',)*2,
                                       ('.state.Any',)*2])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM