| 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 |
from traits.api import Int, Regex, Str |
| 12 |
from traitsui.api import ( |
| 13 |
Action, |
| 14 |
CancelButton, |
| 15 |
Group, |
| 16 |
Handler, |
| 17 |
LiveButtons, |
| 18 |
ModalButtons, |
| 19 |
OKButton, |
| 20 |
View, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
# ------------------------------------------------------------------------- |
| 25 |
# 'Person' class: |
| 26 |
# ------------------------------------------------------------------------- |
| 27 |
|
| 28 |
|
| 29 |
class Person(Handler): |
| 30 |
|
| 31 |
# ------------------------------------------------------------------------- |
| 32 |
# Trait definitions: |
| 33 |
# ------------------------------------------------------------------------- |
| 34 |
|
| 35 |
name = Str() |
| 36 |
age = Int() |
| 37 |
phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') |
| 38 |
notes = Str() |
| 39 |
|
| 40 |
# ------------------------------------------------------------------------- |
| 41 |
# Handles the 'Annoy' button being clicked: |
| 42 |
# ------------------------------------------------------------------------- |
| 43 |
|
| 44 |
def _annoy_clicked(self, info): |
| 45 |
self.edit_traits( |
| 46 |
view=View(title='Annoying', kind='modal', buttons=['OK']) |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
# ------------------------------------------------------------------------- |
| 51 |
# Run the tests: |
| 52 |
# ------------------------------------------------------------------------- |
| 53 |
|
| 54 |
if __name__ == '__main__': |
| 55 |
AnnoyButton = Action( |
| 56 |
name='Annoy', |
| 57 |
tooltip='Click me to be annoyed', |
| 58 |
enabled_when='age >= 40', |
| 59 |
) |
| 60 |
|
| 61 |
person = Person(name='Bill', age=42, phone='555-1212') |
| 62 |
|
| 63 |
fields = Group('name', 'age', 'phone', 'notes~') |
| 64 |
|
| 65 |
person.notes = ( |
| 66 |
"Should have 6 standard 'live' buttons: Undo, Redo, " |
| 67 |
"Revert, OK, Cancel, Help" |
| 68 |
) |
| 69 |
person.configure_traits( |
| 70 |
view=View(fields, kind='livemodal', buttons=LiveButtons) |
| 71 |
) |
| 72 |
|
| 73 |
person.notes = ( |
| 74 |
"Should have 5 standard 'modal' buttons: Apply, Revert, " |
| 75 |
"OK, Cancel, Help" |
| 76 |
) |
| 77 |
person.configure_traits(view=View(fields, buttons=ModalButtons)) |
| 78 |
|
| 79 |
person.notes = "Should have 2 standard buttons: OK, Cancel" |
| 80 |
person.configure_traits( |
| 81 |
view=View(fields, buttons=[OKButton, CancelButton]) |
| 82 |
) |
| 83 |
|
| 84 |
person.notes = "Should have 1 standard button: OK (enabled when age >= 40)" |
| 85 |
person.configure_traits( |
| 86 |
view=View( |
| 87 |
fields, buttons=[Action(name='OK', enabled_when='age >= 40')] |
| 88 |
) |
| 89 |
) |
| 90 |
|
| 91 |
person.notes = "Should have 1 standard button: OK (visible when age >= 40)" |
| 92 |
person.configure_traits( |
| 93 |
view=View( |
| 94 |
fields, buttons=[Action(name='OK', visible_when='age >= 40')] |
| 95 |
) |
| 96 |
) |
| 97 |
|
| 98 |
person.notes = ( |
| 99 |
"Should have 2 standard buttons: OK, Help (defined when " "age >= 50)" |
| 100 |
) |
| 101 |
person.configure_traits( |
| 102 |
view=View( |
| 103 |
fields, |
| 104 |
buttons=['OK', Action(name='Help', defined_when='age >= 50')], |
| 105 |
) |
| 106 |
) |
| 107 |
|
| 108 |
person.notes = ( |
| 109 |
"Should have 1 user and 5 standard buttons: Annoy (enabled " |
| 110 |
"when age >= 40), Apply, Revert, OK, Cancel, Help" |
| 111 |
) |
| 112 |
person.configure_traits( |
| 113 |
view=View(fields, buttons=[AnnoyButton] + ModalButtons) |
| 114 |
) |