Custom view for custom trait

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Custom view for custom trait

Stephan Helma
Hi all,

I wrote a custom trait (basically it handles units). Here is the
relevant part:
        class MyTrait(BaseFloat):
            def validate(self, object, name, value):
                # Convert the value to the SI unit
                value, self.unit = unitconvert(value, self.siunit)
                # Validate the quantity
                value = super(MyTrait, self).validate(object, name, value)
                self.error(object, name, value)

The class is called with the additional keyword 'siunit', eg.
MyTraits(siunit='m'). So far everything works like expected and

Now I want to overwrite create_editor() to make the following editor:
        |input| |value| |self.siunit|
|input| is the input field.
|value| is the return value of validate (the quantity converted into SI
        units). The widget should be read only.
|self.siunit| displays the SI unit associated with the trait
        (self.siunit) and is read only.

I came up with something like this:
        def create_editor(self):
            editors = [super(MyTrait, self).create_editor(),
            return CompoundEditor(editors=editors)
Which looks ugly and does not display the SI unit.

I trawled the Net and the documentation but could not find anything
appropriate to learn from it. Can anybody point me in the right

Any help is welcome!


Enthought-Dev mailing list
[hidden email]