enthought / traitsui
1
# ------------------------------------------------------------------------------
2
#
3
#  Copyright (c) 2005, Enthought, Inc.
4
#  All rights reserved.
5
#
6
#  This software is provided without warranty under the terms of the BSD
7
#  license included in LICENSE.txt and may be redistributed only
8
#  under the conditions described in the aforementioned license.  The license
9
#  is also available online at http://www.enthought.com/licenses/BSD.txt
10
#
11
#  Thanks for using Enthought open source!
12
#
13
#  Author: David C. Morrill
14
#  Date:   09/01/2005
15
#
16
# ------------------------------------------------------------------------------
17

18 4
""" Displays a message to the user as a modal window.
19
"""
20

21 4
from traits.api import HasPrivateTraits, Str, Float
22

23 4
from .view import View
24

25 4
from .group import HGroup
26

27 4
from .item import Item, spring
28

29 4
from pyface.timer.api import do_after
30

31
# -------------------------------------------------------------------------
32
#  'Message' class:
33
# -------------------------------------------------------------------------
34

35

36 4
class Message(HasPrivateTraits):
37

38
    # -------------------------------------------------------------------------
39
    #  Trait definitions:
40
    # -------------------------------------------------------------------------
41

42
    #: The message to be displayed
43 4
    message = Str()
44

45

46
# -------------------------------------------------------------------------
47
#  Displays a user specified message:
48
# -------------------------------------------------------------------------
49

50

51 4
def message(message="", title="Message", buttons=["OK"], parent=None):
52
    """ Displays a message to the user as a model window with the specified
53
    title and buttons.
54

55
    If *buttons* is not specified, a single **OK** button is used, which is
56
    appropriate for notifications, where no further action or decision on the
57
    user's part is required.
58
    """
59 0
    msg = Message(message=message)
60 0
    ui = msg.edit_traits(
61
        parent=parent,
62
        view=View(
63
            ["message~", "|<>"], title=title, buttons=buttons, kind="modal"
64
        ),
65
    )
66 0
    return ui.result
67

68

69 4
def error(message="", title="Message", buttons=["OK", "Cancel"], parent=None):
70
    """ Displays a message to the user as a modal window with the specified
71
    title and buttons.
72

73
    If *buttons* is not specified, **OK** and **Cancel** buttons are used,
74
    which is appropriate for confirmations, where the user must decide whether
75
    to proceed. Be sure to word the message so that it is clear that clicking
76
    **OK** continues the operation.
77
    """
78 0
    msg = Message(message=message)
79 0
    ui = msg.edit_traits(
80
        parent=parent,
81
        view=View(
82
            ["message~", "|<>"], title=title, buttons=buttons, kind="modal"
83
        ),
84
    )
85 0
    return ui.result
86

87

88
# -------------------------------------------------------------------------
89
#  'AutoCloseMessage' class:
90
# -------------------------------------------------------------------------
91

92

93 4
class AutoCloseMessage(HasPrivateTraits):
94

95
    #: The message to be shown:
96 4
    message = Str("Please wait")
97

98
    #: The time (in seconds) to show the message:
99 4
    time = Float(2.0)
100

101 4
    def show(self, parent=None, title=""):
102
        """ Display the wait message for a limited duration.
103
        """
104 0
        view = View(
105
            HGroup(
106
                spring,
107
                Item("message", show_label=False, style="readonly"),
108
                spring,
109
            ),
110
            title=title,
111
        )
112

113 0
        do_after(
114
            int(1000.0 * self.time),
115
            self.edit_traits(parent=parent, view=view).dispose,
116
        )
117

118

119
# -------------------------------------------------------------------------
120
#  Displays a user specified message that closes automatically after a
121
#  specified time interval:
122
# -------------------------------------------------------------------------
123

124

125 4
def auto_close_message(
126
    message="Please wait", time=2.0, title="Please wait", parent=None
127
):
128
    """ Displays a message to the user as a modal window with no buttons. The
129
        window closes automatically after a specified time interval (specified
130
        in seconds).
131
    """
132 0
    msg = AutoCloseMessage(message=message, time=time)
133 0
    msg.show(parent=parent, title=title)

Read our documentation on viewing source code .

Loading