Esempio n. 1
0
def confirm(text='',
            title='',
            buttons=(pymsgbox.OK_TEXT, pymsgbox.CANCEL_TEXT),
            root=None,
            timeout=None,
            icon=QUESTION,
            _tkinter=False):
    """Displays a message box with OK and Cancel buttons. Number and text of buttons can be customized. Returns the text of the button clicked on."""
    buttonFlag = None
    if len(buttons) == 1:
        if buttons[0] == pymsgbox.OK_TEXT:
            buttonFlag = MB_OK
    elif len(buttons) == 2:
        if buttons[0] == pymsgbox.OK_TEXT and buttons[
                1] == pymsgbox.CANCEL_TEXT:
            buttonFlag = MB_OKCANCEL
        elif buttons[0] == pymsgbox.YES_TEXT and buttons[1] == pymsgbox.NO_TEXT:
            buttonFlag = MB_YESNO
        elif buttons[0] == pymsgbox.RETRY_TEXT and buttons[
                1] == pymsgbox.CANCEL_TEXT:
            buttonFlag = MB_RETRYCANCEL
    elif len(buttons) == 3:
        if buttons[0] == pymsgbox.ABORT_TEXT and buttons[
                1] == pymsgbox.RETRY_TEXT and buttons[
                    2] == pymsgbox.IGNORE_TEXT:
            buttonFlag = MB_ABORTRETRYIGNORE
        elif buttons[0] == pymsgbox.CANCEL_TEXT and buttons[
                1] == pymsgbox.TRY_AGAIN_TEXT and buttons[
                    2] == pymsgbox.CONTINUE_TEXT:
            buttonFlag = MB_CANCELTRYCONTINUE
        elif buttons[0] == pymsgbox.YES_TEXT and buttons[
                1] == pymsgbox.NO_TEXT and buttons[2] == pymsgbox.CANCEL_TEXT:
            buttonFlag = MB_YESNOCANCEL

    if (_tkinter) or (timeout is not None) or (buttonFlag is None):
        # Call the original tkinter confirm() function, not this native one:
        return pymsgbox._confirmTkinter(text, title, buttons, root, timeout)

    retVal = messageBoxFunc(0, text, title,
                            buttonFlag | MB_SETFOREGROUND | MB_TOPMOST | icon)
    if retVal == IDOK or len(buttons) == 1:
        return pymsgbox.OK_TEXT
    elif retVal == IDCANCEL:
        return pymsgbox.CANCEL_TEXT
    elif retVal == IDYES:
        return pymsgbox.YES_TEXT
    elif retVal == IDNO:
        return pymsgbox.NO_TEXT
    elif retVal == IDTRYAGAIN:
        return pymsgbox.TRY_TEXT
    elif retVal == IDRETRY:
        return pymsgbox.RETRY_TEXT
    elif retVal == IDIGNORE:
        return pymsgbox.IGNORE_TEXT
    elif retVal == IDCONTINUE:
        return pymsgbox.CONTINUE_TEXT
    elif retVal == IDABORT:
        return pymsgbox.ABORT_TEXT
    else:
        assert False, 'Unexpected return value from MessageBox: %s' % (retVal)
Esempio n. 2
0
 def test_timeout(self):
     # Note: If these test's fail, the unit tests will hang.
     self.assertEqual(pymsgbox._alertTkinter('timeout test', timeout=300),
                      pymsgbox.TIMEOUT_RETURN_VALUE)
     self.assertEqual(pymsgbox._confirmTkinter('timeout test', timeout=300),
                      pymsgbox.TIMEOUT_RETURN_VALUE)
     self.assertEqual(pymsgbox.prompt('timeout test', timeout=300),
                      pymsgbox.TIMEOUT_RETURN_VALUE)
     self.assertEqual(pymsgbox.password('timeout test', timeout=300),
                      pymsgbox.TIMEOUT_RETURN_VALUE)