how to disable OK button when invalid input in UI

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

how to disable OK button when invalid input in UI

Michael Sorich
Any advice would be appreciated on how to disable the OK button
conditional on any of the the traits in the UI being invalid.

The default OK button becomes disabled under most circumstances but
not when an instance trait is included in the view with style='custom'
(see example below - if favourite_number is invalid (e.g. a string)
the input field is highlighted and the OK button disabled, but if the
address.street_number is invalid the field is highlighted but the OK
button is still enabled).

from enthought.traits.api import HasTraits, Str, Instance, Int
from enthought.traits.ui.api import Item, View

class Address ( HasTraits ):
    street_number = Int
    street_name = Str

class Person( HasTraits ):
    name = Str
    favourite_number = Int
    address = Instance( Address, () )

    traits_view = View(['name', 'favourite_number', Item('address',
                        buttons = ['OK','Cancel']

I cannot seem to find the code for how the default OK button is
defined when creating the view - presumably the 'enabled_when' trait
is set to monitor a value on some object (perhaps the UI object) which
flags when one or more of the traits are invalid. OKButton is defined
in traits\ui\, but there is no indication here of how it is
enabled/disabled and I presume the enabled_when trait is set at some
later time. If anyone can point me to the correct file it would be

Thanks in advance
Enthought-Dev mailing list
[hidden email]