Skip to main content
 首页 » 编程设计

python之BoundsEditor 在 TraitsUI 中的使用

2024年10月24日28java哥

我想使用 BoundsEditor(在 TraitsUI 中)进行范围选择。如何访问高值和低值?为了进行测试,我使用了 RangeEditor - 它按预期工作(在移动 slider 时打印当前值)。但是我无法从 BoundsEditor 中获取任何值。感谢任何指点。

我使用以下(简化代码):

from traits.api \ 
    import HasTraits, Button, Range 
from traitsui.api \ 
    import View, Item, Group, RangeEditor 
from traitsui.qt4.extra.bounds_editor import BoundsEditor 
 
class Parameters(HasTraits): 
    rgb_range = Range(0.,1.0) 
    range1 = rgb_range 
    range2 = rgb_range 
    eval_button = Button("Eval")   
 
    traits_view= View( 
        Item('range1')), #editor=RangeEditor() 
        Item('range2', editor=BoundsEditor()), 
        Item('eval_button')) 
 
 
    def _range1_changed(self, value): 
        print(value) 
 
    def _range2_changed(self, *arg, **kwargs): 
        print(arg) 
 
    def _range2_changed(self, *arg, **kwargs): 
        print(arg) 
 
    def _range2_low_changed(self, *arg, **kwargs): 
        print(arg) 
 
    def _range2_high_changed(self, *arg, **kwargs): 
        print(arg) 
 
    def _eval_button_fired(self): 
        print(self.range1) 
        print(self.range2) 
 
 
if __name__ == '__main__': 
    alg = Parameters() 
    alg.configure_traits()  

请您参考如下方法:

我才刚刚开始学习 Traits,所以我相信其他人可以比我更好地解释这一点。我正在使用 http://blog.enthought.com/enthought-tool-suite/traits/new-double-slider-editor/#.VgFbYLTgtWQ 中的示例.我为低值和高值声明了变量,并将它们传递给 BoundsEditor()。然后我声明了当这些值改变时运行的函数。我得到了我认为接近你正在寻找的东西。

from traits.api \ 
    import HasTraits, Button, Range, Float 
from traitsui.api \ 
    import View, Item, Group, RangeEditor 
from traitsui.qt4.extra.bounds_editor import BoundsEditor 
 
class Parameters(HasTraits): 
    rgb_range = Range(0.,1.0) 
    range1 = rgb_range 
    range2 = rgb_range 
    low_val = Float(0.0) 
    high_val = Float(1.0) 
    eval_button = Button("Eval")   
 
    traits_view= View( 
        Item('range1', editor=RangeEditor()), 
        Item('range2', editor=BoundsEditor(low_name = 'low_val', high_name = 'high_val')), 
        Item('eval_button')) 
 
 
    def _range1_changed(self, value): 
        print(value) 
 
    def _low_val_changed(self): 
        print(self.low_val) 
 
    def _high_val_changed(self): 
        print(self.high_val) 
 
    def _eval_button_fired(self): 
        print(self.range1) 
        print(self.low_val) 
        print(self.high_val) 
 
if __name__ == '__main__': 
    alg = Parameters() 
    alg.configure_traits()