Search code examples
pythontkintertkmessagebox

Python tkMessageBox.askyesnocancel -- 3 options, boolean output?


The documentation I have seen on tkMessageBox seems to indicate a boolean return for a user choice on an askyesnocancel dialog. There are 3 options, so how can a boolean properly capture the user choice?

I've tried the approach shown below where a "yes" returns "True", "no" returns "False" and "cancel" returns "cancel", but that doesn't seem to work. A "no" or "cancel" selection both seem to be returned as "False". Anybody have any ideas on this?

if tkMessageBox.askyesnocancel("Error", "Choose yes, no or cancel", default='yes')
    ...
    ...
    ...

elif "cancel":
    return
else:
    pass

Solution

  • Actually, clicking Cancel returns None. Just test this with this line:

    repr(tkMessageBox.askyesnocancel("wa", "wa"))
    

    In conclusion, "Yes" yields True, "No" yields False, and "Cancel" yields None.

    The problem you have there that both the boolean value of None is False, too. You have to explicitly check for None:

     if result is None:
         ...