File: buttons_test.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (114 lines) | stat: -rw-r--r-- 3,421 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from traits.api import Int, Regex, Str
from traitsui.api import (
    Action,
    CancelButton,
    Group,
    Handler,
    LiveButtons,
    ModalButtons,
    OKButton,
    View,
)


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


class Person(Handler):

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

    name = Str()
    age = Int()
    phone = Regex(value='000-0000', regex=r'\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)
    )