2

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
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Qubit1028
  • 307
  • 1
  • 4
  • 12

1 Answers1

8

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:
     ...
TidB
  • 1,749
  • 1
  • 12
  • 14
  • Thanks! I've added a new tool to my belt. – Qubit1028 Feb 28 '15 at 14:57
  • Actually, when I updated the code above with "elif None:" it still did not behave correctly. It seemed to still handle any button other than "yes" as "False". – Qubit1028 Feb 28 '15 at 15:05
  • 1
    That can't work; you still have to do a comparison there, what do you think which boolean value `None` corresponds to? ;) So just assign the result of the messagebox to a variable and insert `if result is None` instead. – TidB Feb 28 '15 at 15:08