2

OK, so I've found in TraitsUI that there are functions for error() and message() that place text into a 'MessageBox' along with optional buttons (like OK and/or Cancel).

How do I trap for the OK or Cancel button being pressed from a message() or error() call?

I was asked to submit an example...here's a simple little one I threw together:

from traits.api import HasTraits,Button

from traitsui.api import View, Group, Item

from traitsui.message import auto_close_message, error, message



class Testing(HasTraits):
    EBtn   = Button( 'make error msg')
    MBtn   = Button( 'make message msg')
    ACBtn  = Button( 'make AC message')

    view = View(
        Group(
            Item ("EBtn", show_label=False),
            Item ("MBtn", show_label=False),
            Item ("ACBtn", show_label=False),
        )
    )

    def _EBtn_fired(self):
        """create an error() message"""
        rtn = error('This is an error() message', 'test message')
        auto_close_message('error() button hit was ' + repr(rtn), 2.0, 'Button Return')

    def _MBtn_fired(self):
        """create a message() message"""
        rtn = message('This is a message() message', 'test message', buttons=['OK', 'Cancel', 'Something Different'])
        auto_close_message('message() button hit was ' + repr(rtn), 3.0, 'Button Return')

    def _ACBtn_fired(self):
        """create an auto_close_message() message"""
        rtn = auto_close_message('This is an auto_close_message() message', 2.0, 'test message')



# Run the program (if invoked from the command line):
if __name__ == '__main__':
    # Create the dialog:
    testDialog = Testing()

    # put the actual dialog up...
    testDialog.configure_traits()

If you click the middle button (for a Message Box), it has 3 buttons...OK, Cancel, and Something Different...The Message returns if OK or Cancel are hit, and I can test the return value, however, if my custom button (Something Different) is clicked, nothing happens? How can I tell if my custom button is pressed?

Steve76063
  • 317
  • 2
  • 10

0 Answers0