0

我正在使用 Enthought 的 TraitsUI 设计一个 UI,但我不知道如何完成我想要的...

这就是我想要的:我在视图中有 Items() 我想显示为英制或 SI 单位。我可以根据 SI/English 按钮更改“编辑”框中的值,但我不知道如何更改标签的文本。例如,如果我有一个项目“长度,英尺 [3.28]”并将其转换为 SI,我希望它显示“长度,米 [1.00]”。我可以处理 3.28->1.00 转换,但不知道如何将“ft”更改为“m”。

有什么建议么?

我尝试过的一件事是定义一个包含单位名称的字符串(如“m”或“ft”)......然后,在项目中,我将标签设置为:

label = '顶部,'+lengthUnits

这在第一次构建视图时工作正常,但是当我更改单位控件时它不会更新标签。是否有某种方法可以强制视图使用所有新值进行更新?

这是一个小 py 程序,它显示了我正在尝试做的事情(请随意批评我的风格:))。我还将尝试添加一些图像来显示发生了什么:

# NOTE: This version of the code has been modified so that it works as I want it to :)

# Example trying to change text on a View...

from traits.api \
    import HasTraits, Enum, CFloat, String

from traitsui.api \
    import View, Group, HGroup, Item, spring

class TestDialog ( HasTraits ):
    length = CFloat(1.0)
    choose_units = Enum('English', 'SI')
    current_units = 'English'
    unit_name = String('ft')
    ft_to_m = CFloat(3.28)
    
    view = View(
        Group(
            HGroup(
                spring,
                Item(name = "length", label = 'Test length'),
                Item(name = 'unit_name', style = 'readonly', show_label = False),
                spring
            ),
            HGroup(
                spring,
                Item(name = "choose_units"),
                spring
            )
        ),
        title = 'Test Changing View Test'
    )

    def _choose_units_changed(self):
        if self.current_units != self.choose_units:
            if self.choose_units == 'SI':
                self.length /= self.ft_to_m
                self.unit_name = 'm'
            else:
                self.length *= self.ft_to_m
                self.unit_name = 'ft'
        self.current_units = self.choose_units
        
# Run the program (if invoked from the command line):
if __name__ == '__main__':
    # Create the dialog:
    TestIt = TestDialog()

    # put the actual dialog up...
    TestIt.configure_traits()

显示我的问题的屏幕截图

4

1 回答 1

1

使用此处所述的通知:http: //code.enthought.com/projects/traits/docs/html/traits_user_manual/notification.html

更新以响应更新的问题:

对,标签不是动态更新的。相反,制作一个看起来像标签的文本字段,例如:

label_text = String('Test length, English:')

然后将其显示在您的视图中,如下所示:

Item("label_text", style='readonly', show_label=False),

您可能还想使用嵌套在 (V)Group 中的 HGroup,将其放置在“长度”显示的左侧。

然后在您的侦听器中修改 label_text 。

于 2014-09-17T21:22:24.560 回答