File: buttons_test.py

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (90 lines) | stat: -rw-r--r-- 3,482 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

from traits.api \
    import *

from traitsui.api \
    import *

from traitsui.menu \
    import *

#-------------------------------------------------------------------------------
#  'Person' class:
#-------------------------------------------------------------------------------

class Person ( Handler ):

    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------

    name  = Str
    age   = Int
    phone = Regex( value = '000-0000', regex = '\d\d\d[-]\d\d\d\d' )
    notes = Str

    #---------------------------------------------------------------------------
    #  Handles the 'Annoy' button being clicked:
    #---------------------------------------------------------------------------

    def _annoy_clicked ( self, info ):
        self.edit_traits( view = View( title   = 'Annoying',
                                       kind    = 'modal',
                                       buttons = [ 'OK' ] ) )

#-------------------------------------------------------------------------------
#  Run the tests:
#-------------------------------------------------------------------------------

if __name__ == '__main__':
    AnnoyButton = Action( name         = 'Annoy',
                          tooltip      = 'Click me to be annoyed',
                          enabled_when = 'age >= 40' )

    person = Person( name = 'Bill', age = 42, phone = '555-1212' )

    fields = Group( 'name', 'age', 'phone', 'notes~' )

    person.notes = ("Should have 6 standard 'live' buttons: Undo, Redo, "
                    "Revert, OK, Cancel, Help")
    person.configure_traits( view = View( fields,
                             kind    = 'livemodal',
                             buttons = LiveButtons ) )

    person.notes = ("Should have 5 standard 'modal' buttons: Apply, Revert, "
                    "OK, Cancel, Help")
    person.configure_traits( view = View( fields,
                                          buttons = ModalButtons ) )

    person.notes = "Should have 2 standard buttons: OK, Cancel"
    person.configure_traits(
               view = View( fields,
                            buttons = [ OKButton, CancelButton ] ) )

    person.notes = "Should have 1 standard button: OK (enabled when age >= 40)"
    person.configure_traits(
               view = View( fields,
                            buttons      = [ Action( name = 'OK',
                            enabled_when = 'age >= 40' ) ] ) )

    person.notes = "Should have 1 standard button: OK (visible when age >= 40)"
    person.configure_traits(
               view = View( fields,
                            buttons      = [ Action( name = 'OK',
                            visible_when = 'age >= 40' ) ] ) )

    person.notes = ("Should have 2 standard buttons: OK, Help (defined when "
                    "age >= 50)")
    person.configure_traits(
               view = View( fields,
                            buttons      = [ 'OK', Action( name = 'Help',
                            defined_when = 'age >= 50' ) ] ) )

    person.notes = ("Should have 1 user and 5 standard buttons: Annoy (enabled "
                    "when age >= 40), Apply, Revert, OK, Cancel, Help")
    person.configure_traits(
               view    = View( fields,
               buttons = [ AnnoyButton ] + ModalButtons ) )