| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Dynamic enabling parts of a user interface using 'enabled_when' |
| 13 |
|
| 14 |
How to dynamically enable or disable components of a Traits UI view, depending |
| 15 |
on the value of another trait. |
| 16 |
|
| 17 |
The demo class "Person" has a set of attributes that apply to all instances |
| 18 |
('first_name', 'last_name', 'age'), a set of attributes that apply only to |
| 19 |
children (Persons whose age is under 18), and a set of attributes that apply |
| 20 |
only to adults. As a Person's age changes, only the age-appropriate attributes |
| 21 |
will be enabled (available for editing). |
| 22 |
|
| 23 |
**Detail:** The optional *enabled_when* attribute of an Item or Group is a |
| 24 |
string containing a boolean expression (logical condition) indicating when this |
| 25 |
Item or Group will be enabled. The boolean expression is evaluated for the |
| 26 |
object being viewed, so that in this example, 'age' refers to the 'age' |
| 27 |
attribute of the Person being viewed. |
| 28 |
|
| 29 |
Compare this to very similar demo of *visible_when*. |
| 30 |
""" |
| 31 |
|
| 32 |
from traits.api import HasTraits, Str, Range, Bool, Enum |
| 33 |
from traitsui.api import Item, Group, View, Label |
| 34 |
|
| 35 |
|
| 36 |
class Person(HasTraits): |
| 37 |
"""Example of enabling/disabling components of a user interface.""" |
| 38 |
|
| 39 |
# General traits: |
| 40 |
first_name = Str() |
| 41 |
last_name = Str() |
| 42 |
age = Range(0, 120) |
| 43 |
|
| 44 |
# Traits for children only: |
| 45 |
legal_guardian = Str() |
| 46 |
school = Str() |
| 47 |
grade = Range(1, 12) |
| 48 |
|
| 49 |
# Traits for adults only: |
| 50 |
marital_status = Enum('single', 'married', 'divorced', 'widowed') |
| 51 |
registered_voter = Bool(False) |
| 52 |
military_service = Bool(False) |
| 53 |
|
| 54 |
# Interface for attributes that are always visible in interface: |
| 55 |
gen_group = Group( |
| 56 |
Item(name='first_name'), |
| 57 |
Item(name='last_name'), |
| 58 |
Item(name='age'), |
| 59 |
label='General Info', |
| 60 |
show_border=True, |
| 61 |
) |
| 62 |
|
| 63 |
# Interface for attributes of Persons under 18: |
| 64 |
child_group = Group( |
| 65 |
Item(name='legal_guardian'), |
| 66 |
Item(name='school'), |
| 67 |
Item(name='grade'), |
| 68 |
label='Additional Info for minors', |
| 69 |
show_border=True, |
| 70 |
enabled_when='age < 18', |
| 71 |
) |
| 72 |
|
| 73 |
# Interface for attributes of Persons 18 and over: |
| 74 |
adult_group = Group( |
| 75 |
Item(name='marital_status'), |
| 76 |
Item(name='registered_voter'), |
| 77 |
Item(name='military_service'), |
| 78 |
'10', |
| 79 |
label='Additional Info for adults', |
| 80 |
show_border=True, |
| 81 |
enabled_when='age >= 18', |
| 82 |
) |
| 83 |
|
| 84 |
# A simple View is sufficient, since the Group definitions do all the work: |
| 85 |
view = View( |
| 86 |
Group( |
| 87 |
gen_group, |
| 88 |
'10', |
| 89 |
Label("Using 'enabled_when':"), |
| 90 |
'10', |
| 91 |
child_group, |
| 92 |
adult_group, |
| 93 |
), |
| 94 |
title='Personal Information', |
| 95 |
resizable=True, |
| 96 |
buttons=['OK'], |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
# Create the demo: |
| 101 |
demo = Person(first_name="Samuel", last_name="Johnson", age=16) |
| 102 |
|
| 103 |
# Run the demo (if invoked from the command line): |
| 104 |
if __name__ == '__main__': |
| 105 |
demo.configure_traits() |