1

In TraitsUI, for an Item(), is it possible to setup a visible_when() parameter list with more than 1 item?

For example, I'd like something like this:

Group(
    Item(FirstItem, visible_when = 'foo == 1 and bar == 0'),
    Item(SecondItem, visible_when = 'foo == 0 and bar == 1),
    Item(ThirdItem, visible_when = 'foo == 0 and bar == 0)
)

So that only FirstItem, SecondItem, or ThirdItem would show up based on the values in foo and bar.

Is this possible? I've made an initial attempt, but had no luck (which may be because one of my logic tests involves a string -- is my problem having a string test inside the visible_when string? This is what my visible_when test looks like (and isn't working):

visible_when = 'Initial_value == 1 && display_units == \'SI\'',

I've tried with the SI in double quotes "SI" and with the escaped single quotes as above...neither worked for me. I've also tried using 'and' and '&&' with no difference either.

Steve76063
  • 317
  • 2
  • 10

1 Answers1

3

Yes, multiple tests work. To verify this, run a slightly modified https://raw.githubusercontent.com/enthought/traitsui/master/examples/demo/Dynamic_Forms/visible_when.py, where you change, for example, the two visible_when conditions to these:

    visible_when = 'age < 18 and last_name=="Smith"',

and

    visible_when = 'age >= 18 or last_name!="Smith"',

I suggest checking assumptions about data contents.

(BTW, strings can use either with either double or escaped single quotes -- I suggest double quotes for readability)

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • I thought that's the way it would work. I looked at your example modification, and it too worked exactly as it should. So I looked more closely at my code...one of the tests was against an enum value, and I have a list that contains all the possible enum elements, and I was testing that list, not the enum value! Thanks for waking me up to my mistake :) – Steve76063 Apr 28 '15 at 20:37